Laravel 5 Mime 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29842625/
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
Laravel 5 Mime validation
提问by Phil Cross
Ok, I'm trying to upload a video, and validate the file type.
好的,我正在尝试上传视频并验证文件类型。
According to the documentation:
根据文档:
mimes:foo,bar,...
The file under validation must have a MIME type corresponding to one of the listed extensions.
Basic Usage Of MIME Rule
'photo' => 'mimes:jpeg,bmp,png'
哑剧:foo,bar,...
验证中的文件必须具有与所列扩展名之一对应的 MIME 类型。
MIME规则的基本用法
'照片' => '哑剧:jpeg,bmp,png'
I'm uploading a wmv video, and my rules are so:
我正在上传一个 wmv 视频,我的规则是这样的:
return [
'file' => ['required', 'mimes:video/x-ms-wmv']
]
I've done a print_r()
on Request::file('file')
and I get the following data:
我已经做了print_r()
的Request::file('file')
,我得到了以下数据:
Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => SampleVideo.wmv
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => video/x-ms-wmv
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 70982901
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => C:\wamp\tmp\php6428.tmp
[fileName:SplFileInfo:private] => php6428.tmp
)
However I'm getting the error:
但是我收到错误:
{"file":["The file must be a file of type: video\/x-ms-wmv."]}
I've tried changing the "mime type" to video/*
, wmv
(as per the docs) and also video/x-ms-wmv
yet none of them validate the file correctly.
我已经尝试将“mime 类型”更改为video/*
, wmv
(根据文档),video/x-ms-wmv
但它们都没有正确验证文件。
As you can see from the print_r()
the mime type Symfony is getting isvideo/x-ms-wmv
.
正如你可以从看到print_r()
MIME类型的Symfony越来越是video/x-ms-wmv
。
Am I doing something wrong? Or can Laravel/Symfony just not validate files well?
难道我做错了什么?或者 Laravel/Symfony 不能很好地验证文件?
I appreciate the help
我感谢帮助
EditOk, I opened validator.php
and added echo $value->guessExtension();
to the ValidateMimes()
method, and it outputs asf.
编辑好的,我打开validator.php
并添加echo $value->guessExtension();
到ValidateMimes()
方法中,它输出asf。
Why is Symfony outputting video\x-ms-wmv
, the file extension is wmv, I'm validating both of them, but Laravel is guessing asf
?!
为什么 Symfony 输出video\x-ms-wmv
,文件扩展名是 wmv,我正在验证它们,但 Laravel 正在猜测asf
?!
It's too unreliable for video validation for me.
对我来说视频验证太不可靠了。
回答by Tim Groeneveld
This is expected behaviour.
这是预期的行为。
Laravel iscalling guessExtension
on Symphony's UploadedFile object, which will return the expected extensionof the file, not the mimetype.
Laravel 正在调用guessExtension
Symphony 的 UploadedFile 对象,该对象将返回文件的预期扩展名,而不是 mimetype。
This is why the documenatation states that for an uploaded image you should use:
这就是为什么文档指出对于上传的图像您应该使用:
'photo' => 'mimes:jpeg,bmp,png'
'照片' => '哑剧:jpeg,bmp,png'
Symfony's guessExtension
calls getMimeType
, which uses PHP's Fileinfo Functionsto go and guess the mimetype of a given file.
Symfony 的guessExtension
调用getMimeType
,它使用 PHP 的Fileinfo 函数来猜测给定文件的 mimetype。
Once getMimeType
guesses the mimetype for the file, Symfony's MimeTypeExtensionGuesserkicks in to get the extension from the mime type retrieved from a file.
一旦getMimeType
猜测了文件的 mimetype,Symfony 的MimeTypeExtensionGuesser 就会开始从文件中检索到的 mime 类型中获取扩展名。
// ... cut from MimeTypeExtensionGuesser
'video/x-ms-asf' => 'asf',
'video/x-ms-wmv' => 'wmv',
'video/x-ms-wmx' => 'wmx',
'video/x-ms-wvx' => 'wvx',
'video/x-msvideo' => 'avi',
Therefore, your rules should be:
因此,您的规则应该是:
return [
'file' => ['required', 'mimes:wmv,asf']
]
The reason that asf
should be included is mainly historical. To quote Wikipedia:
asf
应该包括在内的原因主要是历史原因。引用维基百科:
The most common media contained within an ASF file are Windows Media Audio (WMA) and Windows Media Video (WMV). The most common file extensions for ASF files are extension .WMA (audio-only files using Windows Media Audio, with MIME-type '
audio/x-ms-wma
') and .WMV (files containing video, using the Windows Media Audio and Video codecs, with MIME-type 'video/x-ms-asf
'). These files are identical to the old .ASF files but for their extension and MIME-type.
ASF 文件中包含的最常见媒体是 Windows Media Audio (WMA) 和 Windows Media Video (WMV)。ASF 文件最常见的文件扩展名是扩展名 .WMA(使用 Windows Media Audio 的纯音频文件,带有 MIME 类型“
audio/x-ms-wma
”)和 .WMV(包含视频的文件,使用 Windows Media 音频和视频编解码器,带有 MIME 类型'video/x-ms-asf
')。这些文件与旧的 .ASF 文件相同,但它们的扩展名和 MIME 类型不同。
Microsoft's documentationabout the difference between ASF and WMV/WMA filesstates:
微软关于ASF 和 WMV/WMA 文件区别的文档指出:
The only difference between ASF files and WMV or WMA files are the file extensions and the MIME types [...] The basic internal structure of the files is identical.
ASF 文件与 WMV 或 WMA 文件之间的唯一区别是文件扩展名和 MIME 类型 [...] 文件的基本内部结构是相同的。
Because the internal structure of the file is identical (including the magic numbersfor the file format), wmv, wma and asf are one and the same. The only difference between the three extensions is the icon that is shown inside Explorer.
因为文件的内部结构是相同的(包括文件格式的幻数),所以wmv、wma和asf是一回事。这三个扩展之间的唯一区别是 Explorer 中显示的图标。
It's not just Windows Media files that will have this issue, Wikipedia listsmany different video container formats that will have the same problem. If you want to find the video codecthat is being used in a container, you are going to need to look at more then just the "magic patterns"that are used by the fileinfo
functions.
不仅仅是 Windows Media 文件会出现这个问题,维基百科列出了许多不同的视频容器格式也会有同样的问题。如果您想找到在容器中使用的视频编解码器,您将需要查看更多功能,而不仅仅是功能使用的“魔术模式”fileinfo
。
That being said, expected behaviour!= correct behaviour.
话虽如此,预期行为!=正确行为。
I submitted a pull requestto add a new validator, called mimetypes
. This does as you would expect and uses the guessed mimetype to validate an uploaded file, instead of the extension that is guessed from the mimetype.
我提交了一个拉取请求来添加一个新的验证器,称为mimetypes
. 这与您期望的一样,并使用猜测的 mimetype 来验证上传的文件,而不是从 mimetype 猜测的扩展名。