laravel 验证数组中的多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38326282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:08:09  来源:igfitidea点击:

Validating multiple files in array

laravellaravel-5.2

提问by LaserBeak

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.

我需要验证多个上传的文件,确保它们属于特定类型且小于 2048kb。下面的内容似乎没有检查数组“文件”中的所有文件,只是假设已发布的文件为无效 mime 类型,因为它似乎正在检查数组对象而不是其内容。

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

}

回答by im_tsm

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:

您可以像Laravel 5.2 中的任何输入数组一样验证文件数组。此功能是 Laravel 5.2 中的新功能。您可以执行以下操作:

$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.. 
}

回答by Ismail RBOUH

Please try this:

请试试这个:

public function fileUpload(Request $request) {
    $rules = [];
    $files = count($this->input('files')) - 1;
    foreach(range(0, $files) as $index) {
        $rules['files.' . $index] = 'required|mimes:png,jpeg,jpg,gif|max:2048';
    }

    $validator = Validator::make($request->all() , $rules);

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

回答by Hiren Makwana

Try this way.

试试这个方法。

// getting all of the post data
$files = Input::file('images');

// Making counting of uploaded images
$file_count = count($files);

// start count how many uploaded
$uploadcount = 0;

foreach($files as $file) {
    $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
    $validator = Validator::make(array('file'=> $file), $rules);
        if($validator->passes()){
            $destinationPath = 'uploads';
                $filename = $file->getClientOriginalName();
                $upload_success = $file->move($destinationPath, $filename);
                $uploadcount ++;
        }
}

if($uploadcount == $file_count){
    //uploaded successfully
}
else {
    //error occurred
}

回答by xrkalix

we can also make a request and validate it.

我们也可以提出请求并验证它。

    php artisan make:request SaveMultipleImages

here is the code for request

这是请求的代码

namespace App\Http\Requests;

use App\Core\Settings\Settings;
use Illuminate\Foundation\Http\FormRequest;

class SaveMultipleImages extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
    public function rules()
    {

        return ['files.*' => "mimes:jpg,png,jpeg|max:20000"];
    }
}

and then in controller

然后在控制器中

public function uploadImage(SaveMultipleImages $request) {

     dd($request->all()['files']);
}