Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Uploaded File's Original Name

Tags:

symfony

$request = $event->getRequest();
print_r($request->files);die;

gives me

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

                )

        )

)

I'm trying to get at the value for 'originalname' i.e "Chrysanthemum.jpg" without resorting to a loop, but I can't seem to find the right syntax

Using the 1UP file uploader, but I dont think that's important

like image 407
jriggs Avatar asked Nov 25 '13 20:11

jriggs


People also ask

How can I get file names from uploading?

Try document. getElementById("FileUpload1"). value this value should have a path for a file to be uploaded, just strip all dirs from that value and you will have file name.

How to get name of uploaded file in php?

In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded. The file refers to the name which is defined in the “index. html” form in the input of the file.

How can I upload my file name in laravel 8?

The hashName method is exactly what Laravel calls in the store method. $request->image->hashName(); You will get the same name that Laravel generates when it creates the file name during the store method. $path = $request->image->getClientOriginalName();


2 Answers

When you upload files you get UploadFile (API link) objects (basically the wrappers of array).

$this->getRequest()->files[0]->getClientOriginalName();

Can't try this now but you might need to do this instead:

$this->getRequest()->files['name_of_file_field_in_post']->getClientOriginalName();

where you would replace name_of_file_field_in_post with your form field's name.

like image 172
Jovan Perovic Avatar answered Nov 05 '22 00:11

Jovan Perovic


2015 Update:

$request->files->get('your-file-name')->getClientOriginalName();

your-file-name for me was just file.

This will probably help too: https://github.com/1up-lab/OneupUploaderBundle/issues/21

like image 22
timhc22 Avatar answered Nov 05 '22 00:11

timhc22