file mime type validation if file is uploaded in laravel
13,709
As per Laravel 5.8
For file validation using request data for required, mime type & file size check before upload
$validator = Validator::make($request->all(),[
'image' => 'required|mimes:jpg,jpeg,png,bmp,tiff |max:4096',
],$messages = [
'mimes' => 'Please insert image only',
'max' => 'Image should be less than 4 MB'
]);
dd($validator->errors());
Note: Don't forget to use Validator Facade
Author by
Sagar Gautam
I have completed Bachelors of Computer Engineering from Institute of Engineering Pulchowk Campus. I am a software developer. If you need to contact me feel free to email at [email protected].
Updated on June 19, 2022Comments
-
Sagar Gautam almost 2 years
I have a file upload field as :
<input class="form-control" type="file" name="image[]" multiple="multiple">
This field is optional. User may add file or not.
I want file type validation and I don't want to allow user to add files other than images. This is I have done so far.
$this->validate($request,[ 'image.*' => 'mimes:jpg,jpeg,png,bmp,tiff |max:4096', ],$messages = [ 'mimes' => 'Please insert image only', ]);
But, when there is no file is uploaded, validation doesn't got passed. Any help appreciated.