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
                        )

                )

        )
Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
Rahul
  • 2,189
  • 7
  • 40
  • 60

1 Answers1

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.

devsheeep
  • 2,076
  • 1
  • 22
  • 31
  • thanks for the reply, how can I create objecct $uploadfile, like this $uploadfile= new \Symfony\Component\HttpFoundation\File\UploadedFile(); – Rahul Jun 10 '13 at 09:37
  • 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. – devsheeep Jun 10 '13 at 09:38
  • 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? – Rahul Jun 10 '13 at 10:03
  • ok got it, I guess I have to specify new file name as well along with the $direectory $file = $uploadedFile->move($directory,'test.png'); – Rahul Jun 10 '13 at 10:07
  • 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. http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/File/UploadedFile.html – Donovan Sep 12 '13 at 17:29
  • @Donovan no, answer is not implying to have the same name, we can have the different names for each uploaded file. – Rahul Oct 29 '14 at 06:03