14

Is it possible to upload files in symfony2 WITHOUT using doctrine? With doctrine, its example is given here: http://symfony.com/doc/2.2/cookbook/doctrine/file_uploads.html

Is there a simple way to upload files, like in core PHP we have move_uploaded_file() function with which we can move the uploaded to file after the form is submitted.

For now when I submit the form in symfony this is what I get in 'files' section of request array (Symfony\Component\HttpFoundation\Request)

 [files] => Symfony\Component\HttpFoundation\FileBag Object
        (
            [parameters:protected] => Array
                (
                    [upload_cover] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => secret_of_success.png
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/png
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 157958
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => C:\xampp\tmp\php3636.tmp
                            [fileName:SplFileInfo:private] => php3636.tmp
                        )

                )

        )

1 Answer 1

34

After submitting the form, move the file to your desired directory and you will get a File object in return.

foreach($request->files as $uploadedFile) {
    $name = //...
    $file = $uploadedFile->move($directory, $name);
}

Take a look at the API-Manual for the full featured documentation of this class.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the reply, how can I create objecct $uploadfile, like this $uploadfile= new \Symfony\Component\HttpFoundation\File\UploadedFile();
After submitting the form they will already be around in $request->files;. So you can loop through this array and move every file you sent in the first place.
If I use above for loop, it seems to uploads temp file only, I selected a png for uploading in upload dir I see temp file uploaded not the png image temp file has name php483.tmp. Am I missing something here?
ok got it, I guess I have to specify new file name as well along with the $direectory $file = $uploadedFile->move($directory,'test.png');
Is this answer implying that each file has to have the same "$name"? Here are the methods that can be used to get the filename etc. api.symfony.com/2.0/Symfony/Component/HttpFoundation/File/…
|

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.