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.

3 Answers 3

2

Symfony 2.3 API UploaderFile

The UploaderFile constructor is defined like this:

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

Try using the constructor. And I think that you are using [0] when in your print_r it seems to be ["image"].

If the script stop working on print_r() try using var_dump() instead.

Final code:

    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");
Sign up to request clarification or add additional context in comments.

5 Comments

Still no output, and script blocked, using this: print_r($_FILES); echo 's1'; $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"] ); echo 's2'; var_dump($myfile); die("DEBUG"); I got only S1 after the $_Files output
Do you have error_reporting E_ALL in your php.ini and are using app_dev.php to check that? If there is something wrong it should show an error.
Fatal error: Class 'Done\PunctisBundle\Controller\Symfony\Component\HttpFoundation\File\UploadedFil‌​e' not found in /var/www/src/Done/PunctisBundle/Controller/BrandController.php on line 1431
Add this to the header: use Symfony\Component\HttpFoundation\File\UploadedFile; The use it directly: $myfile= new UploadedFile([...]);
I also took away the ["image"] and add the "use" you suggest and now is working!. Thanks!
2

i think you can do like this:

$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 is the array that contains your gifts_add array

2 Comments

I added the "$" to myfile i suppose you forgot, still im getting im empty output
yeah sorry i forgot :)
0

The easy solution is to make a Symfony Type to bind the data. You still can use your own html form but can catch it in symfony way.

Define a form with your fields:

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

Create and entity in this case called Gift to store your data and recive the upload file.

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

In the controller initialize the form, entity and submit the data:

$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

4 Comments

Can you please elaborate your answer with an example?
Thanks, still I cant get it to work, I think Rachid O. answer is easiest to implement, but then again i cant make it to work, any idea why?
Can you check if $_FILES["gifts_add"]["name"][0] is correct? I think that should be $_FILES["gifts_add"]["name"]["image"].
Is wrong but is not the problem, using app_dev this is the error I got: Fatal error: Class 'Done\PunctisBundle\Controller\Symfony\Component\HttpFoundation\File\UploadedFile' not found in /var/www/src/Done/PunctisBundle/Controller/BrandController.php on line 1431 What do you think?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.