访问PHP数组对象保护的属性

时间:2012-08-31 12:46:14

标签: php symfony symfony-forms

我正在尝试在Symfony2中上传多个文件。我试图访问以下请求对象,但我无法获取参数属性。如何逐个访问文件以上传文件。

我得到的错误:

  

致命错误:无法访问第66行/var/www/File/src/Webmuch/FileBundle/Entity/File.php中的受保护属性Symfony \ Component \ HttpFoundation \ FileBag :: $参数

请求对象:

Symfony\Component\HttpFoundation\FileBag Object
(
    [parameters:protected] => Array
        (
            [file] => Array
                (
                    [0] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => FLB1.jpg
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 13584
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => /tmp/phpzdaQgW
                            [fileName:SplFileInfo:private] => phpzdaQgW
                        )

                    [1] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => FLB2.gif
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/gif
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 5193
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => /tmp/phpNUXho7
                            [fileName:SplFileInfo:private] => phpNUXho7
                        )

                )

        )

)

请帮我弄清楚这个对象的访问权限。此外,如果有人有更好的方法在Symfony2上传文件。

2 个答案:

答案 0 :(得分:4)

要访问这些文件,您可以执行类似

的操作
// retrieves an instance of UploadedFile
$request->files->get('file');

答案 1 :(得分:1)

我认为您正试图直接访问该物业。您必须使用现有的getter和setter 1

这应该返回文件名:

// retrieves an instance of UploadedFile
$file = $request->files->get('file');
$file_name = $file->getClientOriginalName();