7

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"

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:application/pdf,application/doc,application/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.

Muhammad
  • 604
  • 5
  • 14
  • 29

5 Answers5

25

Make sure your form has an attribute enctype="multipart/form-data"

Also, if you use mimes validation the format must be 'mimes:jpeg,bmp,png' https://laravel.com/docs/5.6/validation#rule-mimes

You can read about the enctype attribute here : What does enctype='multipart/form-data' mean?

Nel
  • 261
  • 3
  • 4
0

In form try put files="true".
In validator try put the validations eg required|mimes:doc,docx,pdf

https://laravel.com/docs/5.4/validation#rule-mimes

Edit: When you use mimes:application/doc, actually is mimetypes:application/doc

wbail
  • 536
  • 1
  • 7
  • 18
0

Hi i had the similar issue , everything was correct after hiting following cmds issue got resolved php artisan cache:clear , php artisan config:clear , php artisan route:clear

Kunal Rajput
  • 664
  • 1
  • 7
  • 21
0

Use validation as below if you are facing any issue with .docx extension files

'cv' => 'mimetypes:application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/octet-stream,application/pdf'

This will help to validate doc, Docx, and pdf files.

To show proper error message use the below code:

public function messages ()
{
    return [
        'cv.mimetypes' => 'The cv file must be a file of type: pdf, doc, docx.'
    ];
}

Reference: Laravel 7

Ranjan Fadia
  • 1,363
  • 1
  • 14
  • 18
0

In case someone gets the same problem, you have to use the file exstension, but not the mime type like so:

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

The full list of the extensions can be found: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types