Laravel keep rejecting m4a audio format, how to fi

2019-09-18 11:18发布

问题:

For the life of me, I just cannot figure out how to allow m4a audio file to pass through with laravel validation

$this->validate($request,[
    'audio' => 'required|mimes:mpga,mp3,mp4a,aac,m4a'
]);

The mp3, mpeg files can be uploaded successfully, but just keep rejecting m4a.

Anyone know which mime type should I include?

To be clear, I'm looking for the guess extension of Audio/x-m4a. All of these (m4a, mp4a, mp4, aac) does not seem to work.

Thanks

回答1:

For Laravel 5.4, try this: Change mimes to mimetypes and use audio/x-m4a instead of m4a.



回答2:

If you dd the file mime type like $this->file('contract_copy')->guessExtension(); it will return something "mp4a" for m4a extension.

So I think you need mp4a extension in validation instead of m4a like,

$this->validate($request,[
    'audio' => 'required|mimes:mpga,mp3,mp4a,aac,mp4a'
]);

For more: check the validator class file in vendor\laravel\framework\src\Illuminate\Validation\Validator.php there you can see how laravel validate against the mime-types.

protected function validateMimes($attribute, $value, $parameters)
{
    if (! $this->isAValidFileInstance($value)) {
        return false;
    }

    return $value->getPath() != '' && in_array($value->guessExtension(), $parameters);
}