【问题标题】:How to validate multiple images input?如何验证多个图像输入?
【发布时间】:2017-07-07 13:20:52
【问题描述】:

我有这个功能,允许用户上传一张或多张图片。我已经创建了验证规则,但无论输入是什么,它都会返回 false。

规则:

public function rules()
    {
        return [
            'image' => 'required|mimes:jpg,jpeg,png,gif,bmp',
        ];
    }

上传方式:

public function addIM(PhotosReq $request) {
        $id = $request->id;
        // Upload Image
        foreach ($request->file('image') as $file) {
            $ext = $file->getClientOriginalExtension();
            $image_name = str_random(8) . ".$ext";
            $upload_path = 'image';
            $file->move($upload_path, $image_name);
            Photos::create([
                'post_id' => $id,
                'image' => $image_name,
            ]);
        }
        //Toastr notifiacation
        $notification = array(
            'message' => 'Images added successfully!',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }

如何解决这个问题? 就是这样,谢谢!

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    您有多个图像上传字段名称,例如并将multiple 属性添加到您的输入元素

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

    因此,您的输入就像一个数组,里面会有图像。 由于数组输入验证有不同的方法,请参阅文档here

    因此,您必须验证以下内容:

    $this->validate($request,[
         'image' => 'required',
         'image.*' => 'mimes:jpg,jpeg,png,gif,bmp',
    ]);
    

    希望你明白

    【讨论】:

    • 恭喜兄弟,感觉很好,它帮助了你。所以,请关闭问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2019-06-11
    • 1970-01-01
    • 2015-12-19
    • 2019-08-02
    • 1970-01-01
    • 2015-07-17
    相关资源
    最近更新 更多