gpt4 book ai didi

php - 如何在 Laravel 5.3 中对多张图片上传进行验证

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:00 25 4
gpt4 key购买 nike

我有一个输入

    <input type="file" name="image[]" multiple="multiple" />

和 Controller 函数

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);
}
}

但是验证器总是给我错误

    The image must be an image.

最佳答案

将验证移到循环外并尝试使用规则:

 'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'

关于php - 如何在 Laravel 5.3 中对多张图片上传进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39727463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com