Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does laravels guessExtension() method say that my mp3 file is bin?

I'm trying to upload an mp3 file with laravel. And when I do

dd(Input::file('mp3')->guessExtension());

It gives me back "bin" in a string with certain mp3 files. With other files this is correct, and it is giving me back 'mpga'. What does this "bin" as extension mean?

This file doesn't go through my validation now when I do this rule :

$validator = Validator::make($request->all(), [
            'mp3'      => 'required|mimes:mpga|max:500',
           ]);

When I dump my request it says that the mimetype is mpga , but still it comes back with an error: 'file must be of type mpga'

like image 915
Jim Peeters Avatar asked Jun 03 '16 12:06

Jim Peeters


2 Answers

To answer one of your questions, 'bin' is returned for octet streams:

// Part of the MimeTypeExtensionGuesser class
'application/octet-stream' => 'bin',

Laravel uses Symfony File and Symfony File uses built-in PHP's finfo to guess the mime type.

Therefore, your question becomes – why finfo detects some of your MP3 files as application/octet-stream? There is a possibility that something is slightly wrong about this specific MP3 file, given that other files pass.

By the way, try in a shell:

$ file -I file.mp3 
file.mp3: audio/mpeg; charset=binary

See if you get correct response.

like image 52
Denis Mysenko Avatar answered Sep 28 '22 09:09

Denis Mysenko


You can debug your files using services like

http://mime.ritey.com/

and check if there is any error in validator

if ($validator -> passes()) { // passes } else { dd($validator->errors()->all()); }

If validator is not working as you expect you can try using another validator like http://pastebin.com/raw/NcL5BLwg

Read here: File upload mime-type validation with Laravel 4

like image 35
Diego Betto Avatar answered Sep 28 '22 08:09

Diego Betto