I'm getting a file from storage using:
$image = Storage::get('test.jpg');
How can I get it's mime type?
I've tried:
$mime = Storage::mimeType($image);
With no luck.
I need the mime type so I can return the image as a response:
$response = Response::make($image, 200);
$response->header('Content-Type', $mime);
return $response;
For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.
The mime_content_type() function is an inbuilt function in PHP which is used to get the MIME content-type of a file. Parameters: This function accepts single parameter $file which specifies the path of the file which MIME details to be find. Return Value: This function returns the MIME content type or False on failure.
One of the most important pieces of data, called the MIME type specifies what the body of text describes. For instance, a GIF image is given the MIME type of image/gif, a JPEG image is image/jpg, and postscript is application/postscript.
When using the Storage facade in a controller, you can return the image as a response in Laravel 5.7. The response() function will automatically set the headers required rather than attempting to do it yourself.
// Option #1
return Storage::response('test.jpg'); // Local storage
// Option #2
return Storage::disk('s3')->response('test.jpg'); // If you are using an S3 driver
// Option #3
return Storage::cloud()->response('test.jpg'); // Also S3 driver unless otherwise configured
This is not documented, but can be found by looking at the underlying Laravel code:
/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php
If you still need to get the mime type for any reason, you should be able to retrieve it by calling:
$mimeType = Storage::mimeType('test.jpg');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With