本文介紹了如何在 Laravel 5.3 中對(duì)上傳的多張圖片進(jìn)行驗(yàn)證的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一個(gè)輸入
<input type="file" name="image[]" multiple="multiple" />
和控制器功能
public function upload(Request $request)
{
$user_id = Auth::user()->id;
foreach ($request->image as $image)
{
$this->validate($request,[
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
$imageName = mt_rand() .time().'.'.$image->getClientOriginalExtension();
$img = Image::make($image->getRealPath());
$img->resize(100, 100, function ($constraint) {
$constraint->aspectRatio();
})->save(public_path('images/thumbs').'/'.$imageName);
$image->move(public_path('images'), $imageName);
}
}
但所有時(shí)間驗(yàn)證器都給我錯(cuò)誤
but all the time validator gives me error that
The image must be an image.
推薦答案
將驗(yàn)證移到循環(huán)之外并嘗試使用規(guī)則:
Move validation outside the loop and try with the rules:
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
這篇關(guān)于如何在 Laravel 5.3 中對(duì)上傳的多張圖片進(jìn)行驗(yàn)證的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!