Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing PHP Array Object Protected Property

I am trying to upload multiple files in Symfony2. I am trying to access the following request object, but I am unable get the parameters property. How do I reach the files one by one to upload them.

The error I get:

Fatal error: Cannot access protected property Symfony\Component\HttpFoundation\FileBag::$parameters in /var/www/File/src/Webmuch/FileBundle/Entity/File.php on line 66

Request Object:

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
                        )

                )

        )

)

Please help me figure out this object access. Also if someone has a better way to upload files in Symfony2.

like image 424
Aayush Avatar asked Aug 31 '12 12:08

Aayush


2 Answers

To access the files, you do something like

// retrieves an instance of UploadedFile
$request->files->get('file');
like image 160
Carlos Granados Avatar answered Sep 22 '22 02:09

Carlos Granados


I think you're trying to access the property directly. You must use the existing getters and setters 1

This should return the file name:

// retrieves an instance of UploadedFile
$file = $request->files->get('file');
$file_name = $file->getClientOriginalName();
like image 25
MarcSitges Avatar answered Sep 21 '22 02:09

MarcSitges