我需要验证多个上传的文件,确保它们是特定类型且低于2048kb。下面似乎没有检查数组中的所有文件 'files',只是假定发布的无效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); }
}
答案 0 :(得分:33)
您可以像 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..
}
答案 1 :(得分:3)
请试试这个:
web-app
答案 2 :(得分:1)
试试这种方式。
conn = rpyc.classic.connect(HOST_IP)
答案 3 :(得分:1)
我们还可以提出请求并进行验证。
php artisan make:request SaveMultipleImages
这是请求代码
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"];
}
}
然后在控制器中
public function uploadImage(SaveMultipleImages $request) {
dd($request->all()['files']);
}