Mime type validation in laravel 4 not working

I am trying to check file size and mime type of downloaded file (mp3 file) in laravel. But the check seems to only begin when I upload the image (gif, png). When I upload a 100 MB mkv file, the check seems to be in order with it. Here is my current code:

$file = Input::file('audio_file'); $file_rules = array('audio_file' => 'size:5242880|mimes:mp3'); //also tried mpeg $file_validator = Validator::make(Input::file(), $file_rules); if($file_validator->fails()){ //return validation errors }else{ //always goes here and succeeds } 

Any ideas what is wrong with my code? Thanks in advance!

+1
source share
2 answers

Try changing the file rules line to:

 $file_rules = array('audio_file' => 'size:5242880|mimes:audio/mpeg,audio/mp3,audio/mpeg3'); 

According to this , "audio / mpeg" is the correct MIME type for mp3 files (some browsers also use "audio / mpeg3" or "audio / mp3",).

If this does not work, you can get the MIME type before checking:

 $file = Input::file('audio_file'); $mimeType = $file->getMimeType(); $supportedTypes = ['audio/mpeg', 'audio/mpeg3', 'audio/mp3']; if (in_array($mimeType, $supportedTypes)) { // validate or simply check the file size here } else { // do some other stuff } 
+5
source

The selected answer does not work: $file->getMimeType() returns unpredictable results. Files like .css , .js , .po , .xls and much more get the type text/plain mime. So I posted my solution there fooobar.com/questions/694101 / ...

0
source

All Articles