Get file extension in Laravel

Laravel

I want to get the file extension of one of my uploaded files in Laravel. I've tried to solve this by using the Request $request object in my controller action but it does not seem to work. 

public function uploadFile (Request $request)
{
   // this doesnt work for me.
   $file_extension = $request->input('file_upload')->ext(); 
} 
PHP Programming Question created: 2020-10-10 05:53 BenFlake

3

There is a function called “extension()” on the file object in Laravel. This will return the file extension as a string. It works on every file object. E.g. on the request object or on the file facade itself. It also will work on some storage instances. 

Get file extension on a uploaded file:

$extension = $request->file('file_input')->extension();

Get file extension on a file saved on the filesystem:

$extension = File::get($filePath)->extension()

Get the mime type on a file saved on the filesystem:

$mimeType = File::mimeType($filePath);
answered 2020-10-31 08:55 SpelunkiRU