php Laravel - 视频文件验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22378742/
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 - Video file validation
提问by cch
Is this how to validate a video file in laravel ?
这是如何在 laravel 中验证视频文件?
$validator = Validator::make(Input::all(),
array(
'file' => 'mimes:mp4,mov,ogg | max:20000'
)
);
because even if the file uploaded is a mov type, it will return that the file should be one of the types listed in the rule above.
因为即使上传的文件是 mov 类型,它也会返回该文件应该是上面规则中列出的类型之一。
How I ended up solving it:我最终如何解决它:
As prompted from the answer's below I ended up storing the mime type for the uploaded file to a $mimevariable like so:
按照下面答案的提示,我最终将上传文件的 mime 类型存储到一个$mime变量中,如下所示:
$file = Input::file('file');
$mime = $file->getMimeType();
Then had to write an if statement to check for video mime types:
然后必须编写一个 if 语句来检查视频 MIME 类型:
if ($mime == "video/x-flv" || $mime == "video/mp4" || $mime == "application/x-mpegURL" || $mime == "video/MP2T" || $mime == "video/3gpp" || $mime == "video/quicktime" || $mime == "video/x-msvideo" || $mime == "video/x-ms-wmv")
{
// process upload
}
回答by marcanuy
That code is checking for the extension but not the MIME type. You should use the appropriate MIME type:
该代码正在检查扩展名,而不是 MIME 类型。您应该使用适当的 MIME 类型:
Video Type Extension MIME Type
Flash .flv video/x-flv
MPEG-4 .mp4 video/mp4
iPhone Index .m3u8 application/x-mpegURL
iPhone Segment .ts video/MP2T
3GP Mobile .3gp video/3gpp
QuickTime .mov video/quicktime
A/V Interleave .avi video/x-msvideo
Windows Media .wmv video/x-ms-wmv
If you are not sure the MIME type of the file you are testing you can try $file->getMimeType()
如果您不确定要测试的文件的 MIME 类型,可以尝试 $file->getMimeType()
回答by Данияр Саумбаев
http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
You should add qtin types:
您应该在类型中添加qt:
$validator = Validator::make(Input::all(),
array(
'file' => 'mimes:mp4,mov,ogg,qt | max:20000'
)
);
回答by Conor
For other googlers searching mimes for video validation, use this:
对于其他搜索 mime 以进行视频验证的 googlers,请使用:
'video' => 'mimetypes:video/x-ms-asf,video/x-flv,video/mp4,application/x-mpegURL,video/MP2T,video/3gpp,video/quicktime,video/x-msvideo,video/x-ms-wmv,video/avi'
回答by Josh Forbes
I know this is an older question, but I just ran into the same problem today. Laravel would not validate a .mov filetype, even if I had 'mov', 'quicktime', or 'video/quicktime' in the validator. getMimeType() on the file was showing 'video/quicktime' so it seemed really odd.
我知道这是一个较旧的问题,但我今天遇到了同样的问题。Laravel 不会验证 .mov 文件类型,即使我在验证器中有“mov”、“quicktime”或“video/quicktime”。文件上的 getMimeType() 显示“视频/快速时间”,所以看起来很奇怪。
Digging into Laravel's validator class, it seemed that it was using guessExtension() instead of getMimeType() to check the file. When I put 'qt' as the mime type into the validator (which is what guessExtension() was returning) it worked.
深入研究 Laravel 的验证器类,它似乎使用 guessExtension() 而不是 getMimeType() 来检查文件。当我将 'qt' 作为 mime 类型放入验证器(这是 guessExtension() 返回的)时,它起作用了。
回答by urielOn
i am giving you the following extension MIME typesfor laravel validation
我为您提供以下用于 Laravel 验证的扩展 MIME 类型
回答by vignesh waran
Use this meme type
使用这种模因类型
$this->validate($req, [
'video' => 'required|mimes:mp4,ogx,oga,ogv,ogg,webm',
]);
This format working good for OGG file upload.
这种格式适用于 OGG 文件上传。
回答by Eka putra
after all of the sample answers, you're just doing something like this
在所有示例答案之后,您只是在做这样的事情
if (false !== mb_strpos($image->getMimeType(), "image")) {
echo('image');
} else {
echo('video/something');
}

