gpt4 book ai didi

php - Laravel 文件下载 - 未启用 php_fileinfo 扩展

转载 作者:可可西里 更新时间:2023-11-01 01:14:13 24 4
gpt4 key购买 nike

我正在使用 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 给出的错误:

LogicException in MimeTypeGuesser.php line 135:
Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)

我联系了网络托管公司,他们告诉我,由于安全措施,他们无法启用此扩展程序。

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

最佳答案

如果你想以“laravel 方式”来做,你有一个选择。

Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser 中有一个名为 guess() 的方法,它会被调用并抛出您收到的错误。

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

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'));

您可以查看 vendor/symfony/http-foundation/File/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

关于php - Laravel 文件下载 - 未启用 php_fileinfo 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44051722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com