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

根据文件:

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'

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

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

我对Request::file('file')做了print_r(),我得到了以下数据:

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.

正如你从print_r()中看到的,mime类型Symfony得到了is video/x-ms-wmv.

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

I appreciate the help

Edit Ok, I opened validator.php and added echo $value->guessExtension(); to the ValidateMimes() method, and it outputs 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?!

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

推荐答案

这是意料之中的行为.

Laravel is对Symphony的UploadedFile对象调用guessExtension,该对象将返回预期的文件extension,而不是mimetype.

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

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

symfony的guessExtension调用getMimeType,后者使用PHP的Fileinfo Functions猜测给定文件的mimetype.

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

Therefore, your rules should be:

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

应该包括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.

Microsoft's documentation about the difference between ASF and WMV/WMA files states:

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.

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文件会有这个问题,Wikipedia lists多种不同的视频容器格式也会有同样的问题.如果您想要查找容器中正在使用的video codec,您需要查看的不仅仅是fileinfo个函数使用的"magic patterns".


也就是说,expected behaviour!=correct behaviour.

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相关问答推荐

如何更改LIVE上的Filament占位符内容?

Laravel mail send Amazon SES不再支持TLS 1.0和TLS 1.1连接

自定义验证在表单请求上始终返回 true laravel

运行Docker时,安装PHP8.1时返回错误

Laravel 5.3:语法错误或访问冲突:1463 HAVING 子句中使用了非分组字段距离

如何为路由 laravel 5 使用OR中间件

Laravel 控制器中一种特定方法的中间件

如何处理 Laravel 的 SMTP 驱动程序中的自签名 TLS 证书?

Laravel 扩展验证自定义消息

Laravel 验证 - 输入必须是数组中的项目之一

Laravel whereDoesntHave() - 多个 OR 条件

将 Laravel Socialite 与 API 一起使用?

如何处理 laravel 5 中的私有图像?

如何在 Laravel 5.1 中为Electron邮件添加标题

在 Eloquent 中按自定义顺序对集合进行排序

使用 Laravel 创建新项目会引发异常

Laravel 5 Carbon 全局语言环境

Laravel php artisan 产生错误

Laravel 4 - 文件上传

Laravel Mix:更新 Node.js 依赖项