ehmhrgelcighsawmlv
ehmhrgelcighsawmlv

Reputation: 309

How to get extension and temp path in laravel

I have a file upload function in laravel. The user gets to select multiple files so I need to separate the images file and the documents so I could compress the images. Therefore I need to get the extensions but it just doesn't work for me. I'm using Laravel 7.3 and these are the codes I tried:

$request->files->getClientOriginalExtension();

$request->files->extension();

I also tried to get the temp path using:

$request->files->getPathName()

This is the array returned by $request->files:

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] => Annotation 2020-02-26 084917.png
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/png
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => C:\xampp\tmp\php6AA9.tmp
                            [fileName:SplFileInfo:private] => php6AA9.tmp
                        )
                )
        )
)

I also want to get the value from [pathName:SplFileInfo:private] so I could compress the image but all the codes I stated that I tried above return the same error:

message: "Call to undefined method Symfony\Component\HttpFoundation\FileBag::getClientOriginalExtension()"

Edit:

I also tried

foreach($request->files as $file) {
   foreach ($file as $in) {
    print_r($in->extension());
   }
}

But would give an error Call to undefined method Symfony\Component\HttpFoundation\File\UploadedFile::extension()

Upvotes: 0

Views: 1972

Answers (1)

Salman Zafar
Salman Zafar

Reputation: 4035

Since $request->files is array of files so you have to loop through the files array to get the required result:

foreach($request->files as $file)
{
   echo $file->getClientOriginalExtension();
   echo $file->getPathName();
}

Hope it helps.

Thanks.

Upvotes: 3

Related Questions