2017-06-07 62 views
0

上传我有一个文件上传字段为:文件的MIME类型的验证,如果文件是在laravel

<input class="form-control" type="file" name="image[]" multiple="multiple"> 

此字段是可选的。用户可以添加或不添加文件。

我想要文件类型验证,我不想让用户添加图像以外的文件。这是我迄今为止所做的。

$this->validate($request,[ 
     'image.*' => 'mimes:jpg,jpeg,png,bmp,tiff |max:4096', 
    ],$messages = [ 
     'mimes' => 'Please insert image only', 
    ]); 

但是,当没有文件上传时,验证不会通过。任何帮助赞赏。

+0

让角落找寻工作,如果文件不为空$ VAL =“默剧:JPG,JPEG,PNG,BMP,TIFF | MAX: 4096' ; else''并将$ val放入'image。*'=> $ val中, – Bas

回答

1

附加 '形象'=> '需要', 像

$this->validate($request,[ 
     'image' => 'required', 
     'image.*' => 'mimes:jpg,jpeg,png,bmp,tiff |max:4096', 
    ],$messages = [ 
     'mimes' => 'Please insert image only', 
    ]);