symfony2 how to upload file without doctrine?

烈酒焚心 提交于 2019-11-30 02:31:58

问题


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:


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.



来源:https://stackoverflow.com/questions/17019231/symfony2-how-to-upload-file-without-doctrine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!