0

I have a pretty good Symfony function that makes some file operations from a posted form (generated on symfony), the input of this function is a UploadedFile Object and an example of this object (print_r) is:

Symfony\Component\HttpFoundation\File\UploadedFile Object
(
    [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
    [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Untitled-10001.png
    [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/png
    [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 8718
    [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
    [pathName:SplFileInfo:private] => /tmp/phpduxlg4
    [fileName:SplFileInfo:private] => phpduxlg4
)

I have another web form, pure html with no Symfony implementations or integration. So the posted file from a form like this one as you know will have this form (print_r):

Array
(
    [gifts_add] => Array
        (
            [name] => Array
                (
                    [image] => Untitled-10001.png
                )

            [type] => Array
                (
                    [image] => image/png
                )

            [tmp_name] => Array
                (
                    [image] => /tmp/phpduxlg4
                )

            [error] => Array
                (
                    [image] => 0
                )

            [size] => Array
                (
                    [image] => 8718
                )

        )

)

I cant modify the function that handles the file, so I need a way to "transform" the normal upload file object fron the example two into the Symfony uploadFile object from the first one.

I know is a hack so not a very clean solution, but at the moment is my only way of solving this, so in resume, if you have an object like the second one, how you make it to look like the first one?

EDIT:

Based on the answers this is what i got so far:

        print_r($_FILES);
        print_r($gift->getImage( ));

        echo '---';

        $myfile= new \Component\HttpFoundation\File\UploadedFile();
        $myfile->setOriginalName($_FILES["gifts_add"]["name"][0]);
        $myfile->setMimeType($_FILES["gifts_add"]["type"][0]);
        $myfile->setSize($_FILES["gifts_add"]["size"][0]);
        $myfile->setError($_FILES["gifts_add"]["error"][0]);
        $myfile->setPathName($_FILES["gifts_add"]["tmp_name"][0]);
        $myfile->setFileName($_FILES["gifts_add"]["name"][0]); 


        print_r($myfile); 

        die('DEBUG');    

Still the print_r($myfile) seems to output empty or to block the script.

4

3 回答 3

2

我认为你可以这样做:

$myfile= new \Component\HttpFoundation\File\UploadedFile()
$myfile->setOriginalName($array["gifts_add"]["name"][0]);
$myfile->setMimeType($array["gifts_add"]["type"][0]);
$myfile->setSize($array["gifts_add"]["size"][0]);
$myfile->setError($array["gifts_add"]["error"][0]);
$myfile->setPathName($array["gifts_add"]["tmp_name"][0]);
$myfile->setFileName($array["gifts_add"]["name"][0]); // if you want to keep the original name

$array 是包含您的 gift_add 数组的数组

于 2013-09-13T16:11:00.947 回答
2

Symfony 2.3 API UploaderFile

UploaderFile 构造函数定义如下:

__construct(string $path, string $originalName, string $mimeType = null, integer $size = null, integer $error = null, Boolean $test = false)

尝试使用构造函数。而且我认为您在 print_r 中使用 [0] 时它似乎是 ["image"]。

如果脚本停止在 print_r() 上运行,请尝试改用 var_dump()。

最终代码:

    print_r($_FILES);

    echo '---';
    $myfile= new \Component\HttpFoundation\File\UploadedFile(
        $_FILES["gifts_add"]["tmp_name"]["image"],
        $_FILES["gifts_add"]["name"]["image"],
        $_FILES["gifts_add"]["type"]["image"],
        $_FILES["gifts_add"]["size"]["image"],
        $_FILES["gifts_add"]["error"]["image"]
    );

    var_dump($myfile);
    die("DEBUG");
于 2013-09-14T12:16:13.807 回答
0

简单的解决方案是创建一个 Symfony 类型来绑定数据。你仍然可以使用自己的 html 表单,但可以用 symfony 的方式捕获它。

使用您的字段定义表单:

$builder->add('gifts_add', 'file')

在这种情况下创建一个名为 Gift 的实体来存储您的数据并接收上传文件。

Class Gift {
    public getGiftsAdd(Symfony\Component\HttpFoundation\File\UploadedFile $file) {
        //call your function here
    }
....

在控制器中初始化表单、实体并提交数据:

$gift=new Gift();
$form=$this->createForm(new YourGiftsType(), $entity);
$form->submit($gift);
$gift <--- your data is there now and the form automatically call your funciton
于 2013-09-13T16:02:47.717 回答