在Laravel 4中替换File :: mime()(以从文件扩展名获取mime类型) [英] Replacement for File::mime() in Laravel 4 (to get mime type from file extension)
问题描述
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:
调用未定义的方法Illuminate \ Filesystem \ Filesystem :: mime()
在文件系统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)?
推荐答案
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 Symfony\Component\HttpFoundation\File\File($path);
$mime = $file->getMimeType();
实际上,File类使用 Symfony MimeTypeGuesser 类所以这也可行:
And in fact the File class uses the Symfony MimeTypeGuesser class so this also works:
$guesser = Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser::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类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!