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.
To access the files, you do something like
// retrieves an instance of UploadedFile
$request->files->get('file');
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With