验证数组中的多个文件 [英] Validating multiple files in array

查看:68
本文介绍了验证数组中的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证多个上传的文件,确保它们是特定类型且小于2048kb.以下内容似乎并未检查数组中的所有文件 'files'并仅假定发布的mime类型无效的文件,因为它似乎在检查数组对象而不是其内容.

I need to validate multiple uploaded files, making sure they are of a specific type and under 2048kb. The below doesn't appear to check all files in the array 'files' and just presumes the posted files of invalid mime type as it seems to be checking the array object and not its contents.

public function fileUpload(Request $request)
    {

       $validator = Validator::make($request->all(), [
            'files' => 'required|mimes:jpeg,jpg,png',
        ]);

        if ($validator->fails())
        {
            return response()->json(array(
                'success' => false,
                'errors' => $validator->getMessageBag()->toArray()

            ), 400);             }

}

推荐答案

您可以像 Laravel 5.2 中的任何输入数组一样验证文件数组.此功能是Laravel 5.2中的新增功能.您可以按照以下步骤操作:

You can validate file array like any input array in Laravel 5.2. This feature is new in Laravel 5.2. You can do like following:

$input_data = $request->all();

$validator = Validator::make(
    $input_data, [
    'image_file.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
    ],[
        'image_file.*.required' => 'Please upload an image',
        'image_file.*.mimes' => 'Only jpeg,png and bmp images are allowed',
        'image_file.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
    ]
);

if ($validator->fails()) {
    // Validation error.. 
}

这篇关于验证数组中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆