0

I am unable to retrieve the originalName during a file upload.

print_r($request->file) returns the following:

Symfony\Component\HttpFoundation\FileBag Object
(
    [parameters:protected] => Array
        (
            [form_name] => Array
                (
                    [backgroundImageFile] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => bg_image.jpg
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 65045
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => /tmp/phpsU3X7a
                            [fileName:SplFileInfo:private] => phpsU3X7a
                        )

                )

        )

)


echo "<br /> Name= ".$this->getRequest()->files['backgroundImageFile']->getClientOriginalName();

Returns:

Fatal error: Call to a member function getClientOriginalName() on a non-object in ../path to the controller

So, how do I get the OriginalName?

0

1 Answer 1

2

Try using the get() method on the FileBag, rather than array access.

E.g.,

$this->getRequest()->files->get("backgroundImageFile")->getClientOriginalName();

Alternatively, if it makes sense in your scenario, use a form upload, then access the uploaded file as form data.

Sign up to request clarification or add additional context in comments.

3 Comments

I still get the Fatal error: Call to a member function getClientOriginalName() on a non-object in ../path to the controller. I just need to get the Orginal File Name before the form is submitted
@kewpe20 Before the form is submitted the file isn't uploaded. The upload happens on form submit, so before then there's nothing to get the name of.
If you can put your code up somewhere for me to take a look, I'll see what's going on in your specific instance.

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.