I have put some mime
type validation on my Laravel controller to validate received file from mobile applications:
'file' => 'required_without:body|file|mimes:png,jpg,jpeg,mp3,mp4,pdf,doc,docx,m4a'
This validation rule is OK for most of files, but when iOS app sends recorded voice, this validation rule doesn't work.
Based on what iOS
developer says, the file's mime type is audio/m4a
but laravel (or maybe PHP) can't detect that.
I checked that $request->file('file')->getClientOriginalExtension()
returns audio/m4a
but $request->file('file')->guessExtension()
function returns null!
Any help is appreciated.
This is what I did and I believe it is a good practice:
After some tracing that what laravel does to guess mimetypes, I noticed that it is registering some ExtensionGuesser classes like vendor/symfony/http-foundation/File/MimeType/MimeTypeExtensionGuesser.php
and there was no audio/m4a
mimetype.
I provided AnotherMimeTypeExtensionGuesser
containing:
protected $defaultExtensions = array(
'audio/m4a' => 'm4a',
'audio/x-m4a' => 'm4a',
'audio/mp4' => 'm4a',
);
And then I registered it in my AppServiceProvider
like this:
ExtensionGuesser::getInstance()->register(new AnotherMimeTypeExtensionGuesser);