Validating multiple files in array

后端 未结 4 1111
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 18:49

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\' an

4条回答
  •  我在风中等你
    2020-12-15 19:09

    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
    }
    

提交回复
热议问题