3

I'm using Laravel 5.4.13 with PHP 7.1 and I migrated to a shared hosting. I'm trying to make the website work, but I'm unable because of a 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 what 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 can't enable this extension because of security measurements.

What alternative I have? Is there any other Laravel/PHP function to download a file? Should I use a different framework?

Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137

1 Answers1

3

If you want to do it the "laravel way", you do have an option.

Inside Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser is a method called guess() which is called and is what throws the error you are receiving.

There is also a register() method that lets you register a new one. According to the code:

By default, all mime type guessers provided by the framework are installed (if available on the current OS/PHP setup).

You can register custom guessers by calling the register() method on the singleton instance. Custom guessers are always called before any default ones.

$guesser = MimeTypeGuesser::getInstance();
$guesser->register(new MyCustomMimeTypeGuesser());

If you want to change the order of the default guessers, just re-register your preferred one as a custom one. The last registered guesser is preferred over previously registered ones.

Re-registering a built-in guesser also allows you to configure it:

$guesser = MimeTypeGuesser::getInstance();
$guesser->register(new FileinfoMimeTypeGuesser('/path/to/magic/file'));

You can look at the default guessers in your vendor/symfony/http-foundation/File/MimeType folder, and make your own version that checks 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

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133