Replacement for File::mime() in Laravel 4 (to get mime type from file extension)(Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型))
问题描述
Laravel 3 有一个 File::mime() 可以很容易地从扩展名中获取文件的 mime 类型的方法:
Laravel 3 had a File::mime() method which made it easy to get a file's mime type from its extension:
$extension = File::extension($path);
$mime = File::mime($extension);
升级到 Laravel 4 时出现错误:
On upgrading to Laravel 4 I get an error:
调用未定义的方法 IlluminateFilesystemFilesystem::mime()
我在 Filesystem API 文档中也看不到任何提及 mime 类型的内容.
在 Laravel 4 中获取文件 mime 类型的推荐方法是什么(请注意这不是用户上传的文件)?
What's the recommended way to get a file's mime type in Laravel 4 (please note this is not a user-uploaded file)?
推荐答案
我发现的一个解决方案是使用 Symfony HttpFoundation File 类(它已经作为依赖包含在 Laravel 4 中):
One solution I've found is to use the Symfony HttpFoundation File class (which is already included as a dependency in Laravel 4):
$file = new SymfonyComponentHttpFoundationFileFile($path);
$mime = $file->getMimeType();
实际上 File 类使用 Symfony MimeTypeGuesser 类所以这也有效:
And in fact the File class uses the Symfony MimeTypeGuesser class so this also works:
$guesser = SymfonyComponentHttpFoundationFileMimeTypeMimeTypeGuesser::getInstance();
echo $guesser->guess($path);
但不幸的是,我得到了意想不到的结果:将路径传递给 css 文件时,我得到的是 text/plain 而不是 text/css.
But unfortunately I'm getting unexpected results: I'm getting text/plain instead of text/css when passing a path to a css file.
这篇关于Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型)


- SoapClient 设置自定义 HTTP Header 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Laravel 仓库 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01