Laravel 5 Mime验证 [英] Laravel 5 Mime validation

查看:317
本文介绍了Laravel 5 Mime验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试上传视频,并验证文件类型.

Ok, I'm trying to upload a video, and validate the file type.

根据文档:

哑剧:foo,bar,...

正在验证的文件必须具有与列出的扩展名之一相对应的MIME类型.

The file under validation must have a MIME type corresponding to one of the listed extensions.

MIME规则的基本用法

'照片'=>'mimes:jpeg,bmp,png'

'photo' => 'mimes:jpeg,bmp,png'

我正在上传wmv视频,规则如下:

I'm uploading a wmv video, and my rules are so:

return [
    'file' => ['required', 'mimes:video/x-ms-wmv']
]

我已经在Request::file('file')上完成了print_r(),并且获得了以下数据:

I've done a print_r() on Request::file('file') and I get the following data:

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
)

但是我遇到了错误:

{"file":["The file must be a file of type: video\/x-ms-wmv."]}

我尝试将"MIME类型"更改为video/*wmv(根据文档),并且还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.

print_r()您可以看到,Symfony获得的哑剧类型是 video/x-ms-wmv.

As you can see from the print_r() the mime type Symfony is getting is video/x-ms-wmv.

我做错什么了吗?还是Laravel/Symfony不能很好地验证文件?

Am I doing something wrong? Or can Laravel/Symfony just not validate files well?

感谢您的帮助

修改 好的,我打开了validator.php并将echo $value->guessExtension();添加到了ValidateMimes()方法中,并输出了asf.

Edit Ok, I opened validator.php and added echo $value->guessExtension(); to the ValidateMimes() method, and it outputs asf.

为什么Symfony输出video\x-ms-wmv,文件扩展名是wmv,我正在验证这两个文件,但是Laravel在猜测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?!

对我来说,视频验证太不可靠了.

It's too unreliable for video validation for me.

推荐答案

这是预期的行为.

Laravel是调用 guessExtension 返回文件的预期扩展名,而不是模仿类型.

Laravel is calling guessExtension on Symphony's UploadedFile object, which will return the expected extension of the file, not the mimetype.

这就是为什么文档说明要求您对上传的图像使用的原因:

This is why the documenatation states that for an uploaded image you should use:

'照片'=>'mimes:jpeg,bmp,png'

'photo' => 'mimes:jpeg,bmp,png'

Symfony的guessExtension调用getMimeType,它使用PHP的 Fileinfo函数去猜测给定文件的模仿类型.

Symfony's guessExtension calls getMimeType, which uses PHP's Fileinfo Functions to go and guess the mimetype of a given file.

一旦getMimeType猜出文件的模仿类型,即Symfony的

Once getMimeType guesses the mimetype for the file, Symfony's MimeTypeExtensionGuesser kicks in to get the extension from the mime type retrieved from a file.

    // ... 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',

因此,您的规则应为:

return [
    'file' => ['required', 'mimes:wmv,asf']
]

应包含asf的原因主要是历史原因.引用维基百科:

The reason that asf should be included is mainly historical. To quote Wikipedia:

ASF文件中包含的最常见媒体是Windows Media Audio(WMA)和Windows Media Video(WMV). ASF文件最常见的文件扩展名是.WMA(使用Windows Media Audio的纯音频文件,MIME类型为'audio/x-ms-wma')和.WMV(使用Windows Media Audio和Video编解码器的包含视频文件, MIME类型'video/x-ms-asf').这些文件与旧的.ASF文件相同,但扩展名和MIME类型相同.

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.

Microsoft的文档关于 ASF与WMV/WMA之间的区别文件状态:

ASF文件和WMV或WMA文件之间的唯一区别是文件扩展名和MIME类型[...]文件的基本内部结构是相同的.

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.

因为文件的内部结构相同(包括 魔术数字 (文件格式),则wmv,wma和asf相同.这三个扩展之间的唯一区别是资源管理器中显示的图标.

Because the internal structure of the file is identical (including the magic numbers for 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.

不仅仅是Windows Media文件会出现此问题,维基百科列表许多不同的视频容器具有相同问题的格式.如果要查找容器中正在使用的视频编解码器,则需要查看更多内容,而不仅仅是魔术模式",由fileinfo函数使用.

It's not just Windows Media files that will have this issue, Wikipedia lists many different video container formats that will have the same problem. If you want to find the video codec that 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.

话虽这么说,预期的行为!= 正确的行为.

我提交了拉请求,以添加一个名为mimetypes的新验证器.可以按照您的期望进行操作,并使用猜测的mimetype验证上传的文件,而不是根据mimetype猜测的扩展名.

I submitted a pull request to 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.

这篇关于Laravel 5 Mime验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆