文件上传使用Laravel 4进行MIME类型验证 [英] File upload mime-type validation with 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 $ c $中移动
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')$ c $
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
inapp/libraries/MimeReader.php
(watch the extension) Extend the Laravel
Validator
(I put it in myBaseController
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 isaudio
) - I can now use
audio
as a rule ! Example:'file' => 'required|audio'
, wherefile
refers toInput::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屋!