Laravel-视频文件验证 [英] Laravel - Video file validation

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

问题描述

这是如何在laravel中验证视频文件吗?

Is this how to validate a video file in laravel ?

$validator = Validator::make(Input::all(),
  array(
    'file'  => 'mimes:mp4,mov,ogg | max:20000'
  ) 
);

因为即使上传的文件是mov类型,它也会返回该文件应为上面规则中列出的类型之一.

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.


我最终如何解决它:

根据下面答案的提示,我最终将上载文件的mime类型存储到$mime变量中,如下所示:

As prompted from the answer's below I ended up storing the mime type for the uploaded file to a $mime variable like so:

$file = Input::file('file');
$mime = $file->getMimeType();

然后必须编写if语句来检查视频mime类型:

Then had to write an if statement to check for video mime types:

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
}

推荐答案

该代码正在检查扩展名,而不是MIME类型.您应该使用适当的MIME类型:

That code is checking for the extension but not the MIME type. You should use the appropriate MIME type:

 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

如果不确定要测试的文件的MIME类型,可以尝试$file->getMimeType()

If you are not sure the MIME type of the file you are testing you can try $file->getMimeType()

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

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