Laravel file upload - php_fileinfo extension not included
I am using Laravel 5.4.13 with PHP 7.1 and I have migrated to shared hosting. I'm trying to make the site work, but I can't because of the missing extension:php_fileinfo
This is the code where the website crashes:
$file = base_path() . "/storage/app/public/small.mp4";
return response()->download($file, "small.mp4")->deleteFileAfterSend(true);
and this is the error that Laravel gives:
LogicException in MimeTypeGuesser.php line 135:
Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)
I contacted the web hosting company and they told me that they cannot enable this extension due to security dimensions.
What is my alternative? Is there any other Laravel / PHP function for uploading the file? Should I use a different framework?
source to share
If you want to make it the "laravel way" you have an option.
Internally Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
, this is the method called guess()
which is called and is what causes the error you are getting.
There is also a method register()
that allows you to register a new one. According to the code:
By default, all the mime-type gadgets provided by the framework are installed (if available in the current OS / PHP setup).
You can register custom fortune tellers by calling the register () method on a single instance. Custom guesses are always called before any defaults.
$guesser = MimeTypeGuesser::getInstance();
$guesser->register(new MyCustomMimeTypeGuesser());
If you want to change the order of the default guessers, just re-register your preferred ones as usual. The latest recorded guess is preferable to the previously recorded one.
Re-registering the built-in guesser also allows you to customize it:
$guesser = MimeTypeGuesser::getInstance();
$guesser->register(new FileinfoMimeTypeGuesser('/path/to/magic/file'));
You can look at the default guesses in your folder vendor/symfony/http-foundation/File/MimeType
and create your own version that checks the mime type in a way that will be supported. Then register it.
See also:
http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.html
https://github.com/symfony/http-foundation/blob/master/File/MimeType/MimeTypeGuesser.php#L131
source to share