tareenmj's avatar

Mimes Validation not working correctly in Laravel

My webpage contains a file for upload, and I want the file uploaded to only be either pdf, doc or docx.

My form tag also has enctype="multipart/form-data" and has files="true"

My html looks like:

<div id="cv_upload" class="row">
    <div class="col-xs-12">
          <input type="file" name='cv'>
    </div>
</div>

The $rules array associated with this is as follows:

'cv' => 'mimes:pdf,doc,docx|required'

And finally my messages looks like:

'cv.required' => 'A selection for C.V. is required before proceeding.',
'cv.mimes' => 'CV must be of the following file type: pdf, doc or docx.'

The only problem with this is, even after I upload a pdf or doc, the message I receive is the one for required. I have no idea why this isn't working as expected. I also tried removing the 'application/' but that yields no success either. Please help.

0 likes
6 replies
zhezhebie's avatar

I come accross this bug either ! and so does the mimetypes doesn't work correctly ! when i validate excel,word and pdf , I use

dd($request->file('document')->getMimeType(),$request->file('document')->getClientOriginalExtension() );

even can't get mimetype correctly ,but sometimes it does .like below :

"application/CDFV2-unknown"
"xls"

@tareenmj @Cronix @rzvme

zhezhebie's avatar

@tareenmj @devEs thank you devEs ,I have seen your answer !

https://laracasts.com/discuss/channels/requests/laravel-5-mimes-validator-always-returning-errors-even-with-right-mime-type

I know this thread is old, but this might help others who may stumbled on the same problem. I actually got the idea from what @rogrido said about checking your images/attachments as arrays. I am using "attachments[]" as input name by the way. And I got it working using this:

$this->validate(request(), [
       'document.*' => 'required|file|mimes:ppt,pptx,doc,docx,pdf,xls,xlsx|max:204800',
]);

using the dot(.) asterisk solved my problem.

10 likes
franciscocaldeira's avatar

@zhezhebie On laravel 8.83.11 having 'document.*' => 'required|file|mimes:ppt,pptx,doc,docx,pdf,xls,xlsx|max:204800', on the request if you don't send the document, the validation rule required don't work with the dot(.) asterisk.

I had to do like this for the required to work:

'document' => 'required',
'document.*' => 'file|mimes:ppt,pptx,doc,docx,pdf,xls,xlsx|max:204800'
1 like
bed's avatar

@zhezhebie Hei, I am stuck on the same problems. Can u upload your solution.

Szach's avatar

@zhezhebie Thank you. Tried this for images as images.* and error on validation got resolved.

rahoo303's avatar

You can use like below

$validated = $request->validate([ 'document' => 'mimetype:image/jpeg,application/octetstream.application/msword.application/vnd.oasis.opendocument.text' ]);

Please or to participate in this conversation.