5

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.

2 Answers 2

5

To access the files, you do something like

// retrieves an instance of UploadedFile
$request->files->get('file');
Sign up to request clarification or add additional context in comments.

3 Comments

But how do we access the private properties without doctrine ?
Doctrine? I did not mention doctrine anywhere in my answer. Can you please try to make your question more clear?
Yes! the thing is these are protected properties , even if you use $request->files->get('file'); that doesn't mean that you have access to these properties because they're protected, the only way is to write getters in an entity class.. this is what documentation says. but if you have other opinion I'll be glad to hear it
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.