Laravel 验证来自 HTML 表单数组的多个图像输入
Laravel validation of multiple images inputs from HTML form array
我在 HTMl 表单中有以下字段,
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
当我提交表单时,我会在 PHP 中获得 images[] 数组。我正在使用 Laravel 框架进行表单验证。我一直在使用验证器和规则。但我想对上述数组中的每个图像应用所需大小和最大大小的规则。谢谢
这是我的图像验证的示例代码:
// Handle upload(s) with input name "files[]" (array) or "files" (single file upload)
if (Input::hasFile('files')) {
$all_uploads = Input::file('files');
// Make sure it really is an array
if (!is_array($all_uploads)) {
$all_uploads = array($all_uploads);
}
$error_messages = array();
// Loop through all uploaded files
foreach ($all_uploads as $upload) {
// Ignore array member if it's not an UploadedFile object, just to be extra safe
if (!is_a($upload, 'Symfony\Component\HttpFoundation\File\UploadedFile')) {
continue;
}
$validator = Validator::make(
array('file' => $upload),
array('file' => 'required|mimes:jpeg,png|image|max:1000')
);
if ($validator->passes()) {
// Do something
} else {
// Collect error messages
$error_messages[] = 'File "' . $upload->getClientOriginalName() . '":' . $validator->messages()->first('file');
}
}
// Redirect, return JSON, whatever...
return $error_messages;
} else {
// No files have been uploaded
}
我在 HTMl 表单中有以下字段,
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
当我提交表单时,我会在 PHP 中获得 images[] 数组。我正在使用 Laravel 框架进行表单验证。我一直在使用验证器和规则。但我想对上述数组中的每个图像应用所需大小和最大大小的规则。谢谢
这是我的图像验证的示例代码:
// Handle upload(s) with input name "files[]" (array) or "files" (single file upload)
if (Input::hasFile('files')) {
$all_uploads = Input::file('files');
// Make sure it really is an array
if (!is_array($all_uploads)) {
$all_uploads = array($all_uploads);
}
$error_messages = array();
// Loop through all uploaded files
foreach ($all_uploads as $upload) {
// Ignore array member if it's not an UploadedFile object, just to be extra safe
if (!is_a($upload, 'Symfony\Component\HttpFoundation\File\UploadedFile')) {
continue;
}
$validator = Validator::make(
array('file' => $upload),
array('file' => 'required|mimes:jpeg,png|image|max:1000')
);
if ($validator->passes()) {
// Do something
} else {
// Collect error messages
$error_messages[] = 'File "' . $upload->getClientOriginalName() . '":' . $validator->messages()->first('file');
}
}
// Redirect, return JSON, whatever...
return $error_messages;
} else {
// No files have been uploaded
}