Php 如果文件上载到laravel中,则文件mime类型验证
我有一个文件上载字段,如下所示:Php 如果文件上载到laravel中,则文件mime类型验证,php,laravel,validation,file-upload,mime,Php,Laravel,Validation,File Upload,Mime,我有一个文件上载字段,如下所示: <input class="form-control" type="file" name="image[]" multiple="multiple"> 但是,当没有上传任何文件时,验证不会通过。感谢您的帮助。根据Laravel 5.8 对于使用请求数据进行文件验证,请在上载之前检查mime类型和文件大小 $validator = Validator::make($request->all(),[ 'image' => '
<input class="form-control" type="file" name="image[]" multiple="multiple">
但是,当没有上传任何文件时,验证不会通过。感谢您的帮助。根据Laravel 5.8 对于使用请求数据进行文件验证,请在上载之前检查mime类型和文件大小
$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());
注意:别忘了使用Validator Facade我可能会检查请求中是否存在该文件,然后对其运行验证。 例如:
如果文件不为空,则在周围制作一个作品$val='mimes:jpg,jpeg,png,bmp,tiff | max:4096';else'',并将$val放入'image.*'=>$val,
$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());
if ($request->has('image') {
// Do your validation check
}