Laravel文件下载 - php_fileinfo扩展名未启用 [英] Laravel file download - php_fileinfo extension not enabled

查看:390
本文介绍了Laravel文件下载 - php_fileinfo扩展名未启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.4.13与PHP 7.1,我迁移到共享托管。我试图使网站正常工作,但由于缺少扩展名,我无法使用: php_fileinfo



这是网站崩溃的代码:

  $ file = base_path()。 /storage/app/public/small.mp4; 
return response() - > download($ file,small.mp4) - > deleteFileAfterSend(true);

这是Laravel给出的错误:



M $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ code>

我联系了网络托管公司,他们告诉我,由于安全性测量,他们无法启用此扩展。



我有什么选择?有没有其他Laravel / PHP功能下载文件?我应该使用不同的框架吗?

解决方案

如果你想做laravel方式,你有一个选择。



Inside Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser 是一种名为 guess()这是被调用,是什么抛出你收到的错误。



还有一个 register()方法可以让你注册一个新的。根据代码:


默认情况下,框架提供的所有mime类型的猜测器都安装
(如果可用的话) OS / PHP设置)。



您可以通过调用
单例实例上的register()方法来注册自定义的猜测器。




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




如果要更改默认猜测器的顺序,只需重新 - 将您的
首选为自定义。最后一个注册的猜测者优先于
以前注册的猜测器。



重新注册内置的猜测器还允许您进行配置:




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

您可以查看 vendor / symfony / http-基础/文件/ MimeType 文件夹,并使您自己的版本检查mime类型的方式将被支持。然后注册。



另请参见:



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


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?

解决方案

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

这篇关于Laravel文件下载 - php_fileinfo扩展名未启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆