Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - get MIME type from file extension

In Laravel 5 how can I get MIME type from extension? Even better if there's a way to convert array of extensions to array of mimes.

E.g. how to convert array('doc', 'xls') to array('application/msword', 'application/vnd.ms-excel')?

like image 621
Limon Monte Avatar asked Apr 08 '15 08:04

Limon Monte


3 Answers

When "guzzlehttp/guzzle": "~5.3|~6.0" is in your composer.json, you can use this:

$mimetype = \GuzzleHttp\Psr7\mimetype_from_filename('foo.doc');
$mimetype = \GuzzleHttp\Psr7\mimetype_from_extension('doc');
like image 144
Juukie14 Avatar answered Oct 17 '22 19:10

Juukie14


Simply the best in L5:

\File::mimeType('physical/path/to/file.ext');
like image 8
Umair Hamid Avatar answered Oct 17 '22 20:10

Umair Hamid


to get file mimetype

$request->file->getMimeType()

validation code

$request->validate([
    'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg'
    'mp3'=>'required|mimetypes:audio/mpeg'
   ]);

you can get the file type from above code, after that you can set that to mimetypes like set for mp3

like image 7
Rana Nadeem Avatar answered Oct 17 '22 19:10

Rana Nadeem