文件上传使用Laravel 4进行MIME类型验证 [英] File upload mime-type validation with Laravel 4

查看:2123
本文介绍了文件上传使用Laravel 4进行MIME类型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我上传一个格式良好的MP3文件时,Laravel 4告诉我它不是 audio / mp3 但是 application / octet-stream $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' trackfile'=> Input :: file('trackfile')),
array('trackfile'=>'required | mimes:mp3')
);

if($ validator-> fails())
return'doesn'''t works because mime type is'.Input :: file('trackfile') - > getMimeType ();
else
return'it works!';

为什么不把它上传为 audio / mp3 file?



(我已经在表单声明中添加了'files'=> true

解决方案

编辑

在验证器仍然在考虑一些MP3作为应用程序/八位字节流,你是否用这个或那个软件转换原始文件。

我终于使用 MimeReader class by user 谢恩,完全有效。我仍然想知道为什么很难发现一个正确的MIME类型。

我在Laravel中实现了它:




  • app / libraries / MimeReader.php MimeReader.phps c>(看扩展)
  • 扩展Laravel Validator (我把它放在我的 BaseController 构造函数)
    $ b $ pre $ Validator :: extend('audio',function($ attribute,$ value) ,$参数)

    $ allowed = array('audio / mpeg','application / ogg','audio / wave','audio / aiff');
    $ mime = new MimeReader($ value-> getRealPath());
    return in_array($ mime-> get_type(),$ allowed);
    });


  • app / lang / [whatever] / validation中添加错误信息。 php (在我的例子中,键是 audio

  • 现在我可以使用音频作为一个规则!例如:'file'=> 'required | audio',其中 file 引用 Input :: file('file')





Laravel正在考虑 audio / mpeg 文件是 .mpga 而不是 .mp3 。我在 MimeTypeExtensionGuesser.php (在Symfony库中)以及 audio / ogg code> .oga 。也许音频MIME类型取决于使用的软件编码器。

其他方法是绕过验证器并使用<$ c直接检查mime类型到控制器中$ c $> Input :: file('upload') - > getMimeType()像Sheikh Heera说的那样。

When I upload a well-formed MP3 file, Laravel 4 tells me it's not audio/mp3 but application/octet-stream, which makes this validation fails:

$validator = Validator::make(
    array('trackfile' => Input::file('trackfile')), 
    array('trackfile' => 'required|mimes:mp3')
);

if($validator->fails())
    return 'doesn\'t works because mime type is '.Input::file('trackfile')->getMimeType();
else
    return 'it works!';

Why doesn't it upload the file as an audio/mp3 file ?

(I already added 'files' => true to the form declaration)

解决方案

Edit

Built-in validator was still considering some mp3's as application/octet-stream, wether you converted the original file with this or that software.

I finally used the MimeReader class by user Shane, which totally works. I'm still wondering why is it so hard to detect a right mime type though.

I implemented it as a validator in Laravel:

  • Move MimeReader.phps in app/libraries/MimeReader.php (watch the extension)
  • Extend the Laravel Validator (I put it in my BaseController constructor)

    Validator::extend('audio', function($attribute, $value, $parameters)
    {
        $allowed = array('audio/mpeg', 'application/ogg', 'audio/wave', 'audio/aiff');
        $mime = new MimeReader($value->getRealPath());
        return in_array($mime->get_type(), $allowed);
    });
    

  • Add your error message in app/lang/[whatever]/validation.php (in my case, the key is audio)
  • I can now use audio as a rule ! Example: 'file' => 'required|audio', where file refers to Input::file('file')

Laravel was considering audio/mpeg files to be .mpga and not .mp3. I corrected it in MimeTypeExtensionGuesser.php (in the Symfony library), along with audio/ogg that were considered as .oga. Maybe the audio mime-type depends on what software encoder was used.

Other method is to bypass the validator and check the mime-type directly into the controller using Input::file('upload')->getMimeType() like Sheikh Heera said.

这篇关于文件上传使用Laravel 4进行MIME类型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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