Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel validation doesnt work for .JPG

In Laravel5 I validate a picture with the following code:

public function rules() {
    return [
        'name' => 'required',
        'pic' => 'required|mimes:jpeg,png',
    ];
}

It works for files with extensions filename.jpg and filename.jpeg but it doesn't for filename.JPG.

However, it doesn't return wrong file extension but a missing file. Can anyone help?

like image 296
dragomir Avatar asked Jan 08 '23 06:01

dragomir


2 Answers

Try this in your pic rules:

'pic' => 'required|mimes:jpeg,jpg,png',

Explanation

Had this problem and found that when you use mimes: rule, you have to specify the extensions you want as valid i. e. you have to add both jpeg and jpg extension variations.

Other solutions

  • Use mimetypes: rule with image/jpeg that covers 3 extension variations for the jpeg format: jpg jpeg jpe.

  • Use image rule which covers jpeg, png, bmp, gif, or svg including jpeg's extension variations

More on the mimes and mimetypes rules: https://laravel.com/docs/5.5/validation#rule-mimetypes

All MIME types:

A full listing of MIME types and their corresponding extensions may be found at the following location: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

like image 113
delroh Avatar answered Jan 10 '23 20:01

delroh


Try 'pic' => 'required|image' and make sure your form has the enctype="multipart/form-data" attribute.

like image 22
Hans Ott Avatar answered Jan 10 '23 19:01

Hans Ott