php Laravel 4 中 File::mime() 的替换(从文件扩展名获取 mime 类型)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19681854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacement for File::mime() in Laravel 4 (to get mime type from file extension)
提问by mtmacdonald
Laravel 3 had a File::mime()method which made it easy to get a file's mime type from its extension:
Laravel 3 有一个File::mime()方法,可以很容易地从其扩展名中获取文件的 mime 类型:
$extension = File::extension($path);
$mime = File::mime($extension);
On upgrading to Laravel 4 I get an error:
升级到 Laravel 4 时出现错误:
Call to undefined method Illuminate\Filesystem\Filesystem::mime()
调用未定义的方法 Illuminate\Filesystem\Filesystem::mime()
I also can't see any mention of mime types in the Filesystem API docs.
我也看不到Filesystem API docs 中任何对 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)?
在 Laravel 4 中获取文件的 mime 类型的推荐方法是什么(请注意这不是用户上传的文件)?
回答by mtmacdonald
One solution I've found is to use the Symfony HttpFoundation File class(which is already included as a dependency in Laravel 4):
我发现的一种解决方案是使用Symfony HttpFoundation File 类(它已作为依赖项包含在 Laravel 4 中):
$file = new Symfony\Component\HttpFoundation\File\File($path);
$mime = $file->getMimeType();
And in fact the File class uses the Symfony MimeTypeGuesserclass so this also works:
事实上 File 类使用Symfony MimeTypeGuesser类,所以这也适用:
$guesser = Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser::getInstance();
echo $guesser->guess($path);
But unfortunately I'm getting unexpected results: I'm getting text/plaininstead of text/csswhen passing a path to a css file.
但不幸的是,我得到了意想不到的结果:将路径传递给 css 文件时,我得到的是text/plain而不是text/css。
回答by Dennis Braga
IF you had just uploaded the file you can use:
如果您刚刚上传了文件,则可以使用:
Input::file('field_name')->getMimeType();
You can see more here! I hope it be for some help! :D
你可以在这里看到更多!我希望它能有所帮助!:D
EDIT:Input::file
is some kind of extention from File
, so you may use File::get('file')->getMimeType();
also. Didn't test, but MUST work.
编辑:Input::file
是从 的某种扩展File
,所以你也可以使用File::get('file')->getMimeType();
。没有测试,但必须工作。
回答by mtmacdonald
After reading that:
读完之后:
- PHP mime_content_type()is deprecated
- Its replacement FileInfois unreliable
- Symfony's getMimeType()uses FileInfo (see my other answer)
- PHP mime_content_type()已弃用
- 它的替代FileInfo不可靠
- Symfony 的getMimeType()使用 FileInfo(请参阅我的其他答案)
I decided instead to port Laravel 3's implementation of File::mime()into a helper library within my Laravel 4 application. The Laravel 3 implementation just reads the MIME types from a config lookup array, based on file extension.
我决定将 Laravel 3 的File::mime()实现移植到我的 Laravel 4 应用程序中的帮助程序库中。Laravel 3 实现只是根据文件扩展名从配置查找数组中读取 MIME 类型。
Solution:
解决方案:
- Copied application/config/mimes.phpfrom my L3 project to app/config/mimes.phpin my L4 project
- Made a FileHelper library with the File::mime() function code from the Laravel 3 File class.
- 复制的application / config / mimes.php从我L3项目中的应用程序/配置/ mimes.php在我的L4项目
- 使用来自Laravel 3 File 类的 File::mime() 函数代码制作了一个 FileHelper 库。
回答by kovpack
It turned out that Symfony ExtensionGuesser
and MimeTypeGuesser
use unreliable FileInfo
class. For that reason validation of mimes return unpredictable results and can not be used with files uploads in a proper way (it returns text/plain
mime for js
, xls
, po
etc.).
原来,SymfonyExtensionGuesser
和MimeTypeGuesser
使用不可靠的FileInfo
类。对于默剧的这个原因验证返回不可预知的结果,并不能以恰当的方式上传文件(它返回使用text/plain
MIME为js
,xls
,po
等)。
I've found very simple solution for this problem.
我为这个问题找到了非常简单的解决方案。
Instead of
代替
'attachment' => 'required|mimes:jpg,jpeg,bmp,png,doc,docx,zip,rar,pdf,rtf,xlsx,xls,txt|max:10000',
I split that into two different parts and now my validation looks like this:
我把它分成两个不同的部分,现在我的验证看起来像这样:
private function createFileAttachmentValidator($file)
{
return Validator::make(
[
'attachment' => $file,
'extension' => \Str::lower($file->getClientOriginalExtension()),
],
[
'attachment' => 'required|max:10000',
'extension' => 'required|in:jpg,jpeg,bmp,png,doc,docx,zip,rar,pdf,rtf,xlsx,xls,txt',
],
$this->validationMessages()
);
}
I simply try to verify that extension of the file is present and that it is listed in my in
rule. That works, however, solution is not perfect.
我只是尝试验证文件的扩展名是否存在以及它是否列在我的in
规则中。这有效,但是,解决方案并不完美。
回答by itsazzad
public function mimeType($path)
{
return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
}
Ref: https://github.com/illuminate/filesystem/blob/master/Filesystem.php#L194
参考:https: //github.com/illuminate/filesystem/blob/master/Filesystem.php#L194