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.