如何将Laravel文件验证用于循环内的多个文件?
通常在单个文件验证中,我可以像
一样$validator = Validator::make($request->all(),[
'image' => 'image|max_width:1000|max_height:1000'
]);
if($validator->fails()){
//validation fails
}
但现在我想在一个进程中验证多个文件,比如
//store to a variable
$file = $request->file('image');
if($file){ //if request file image exist
foreach($request->file('image') as $i){
//here, validate each file image, but it just I don't know how
}
}
现在为前端方面,详细说明在一个帖子中发送多个文件的过程。
$(document).on("change",'input[type="file"]',function(){
if($(this).val()!==""){
var files = this.files;
handleFileUpload(files);
$(this).val("");
}
});
function handleFileUpload(files)
{
//create a formData
var fd = new FormData();
//loop through e
for (var i = 0; i < files.length; i++)
{
//append each file object to the formData
fd.append('image[]', files[i]);
}
//this function is where ajax request will be process
sendFileToServer(fd);
}
所以在后端,如果我转储请求,我得到这些
array(2){[0] =&gt; object(Illuminate \ Http \ UploadedFile)#214(7){ [ “测试”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; 布尔(假) [ “ORIGINALNAME”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; string(50)“12400457_643171672489204_2452820305362127031_n.jpg” [ “mime类型”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; string(10)“image / jpeg” [ “尺寸”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; INT(18413) [ “错误”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; int(0)[“pathName”:“SplFileInfo”:private] =&gt;串(25) “C:\ wamp64 \ tmp \ phpBAE4.tmp”[“fileName”:“SplFileInfo”:private] =&gt; string(11)“phpBAE4.tmp”} [1] =&gt; object(Illuminate \ Http \ UploadedFile)#217(7){ [ “测试”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; 布尔(假) [ “ORIGINALNAME”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; string(39)“13296200_702501119889592_55691535_n.jpg” [ “mime类型”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; string(10)“image / jpeg” [ “尺寸”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; INT(95383) [ “错误”: “的Symfony \元器件\ HttpFoundation \文件\ UploadedFile的”:私人] =&GT; int(0)[“pathName”:“SplFileInfo”:private] =&gt;串(25) “C:\ wamp64 \ tmp \ phpBAE5.tmp”[“fileName”:“SplFileInfo”:private] =&gt; string(11)“phpBAE5.tmp”}}
任何想法,线索,请帮助您了解如何在循环内的多个文件上使用Laravel文件验证?
PS:我在Laravel 5.2上运行
答案 0 :(得分:0)
您可以尝试以下操作:
foreach($this->input('image') as $key => $value){
$rules['image.'.$key] = 'image|max_width:1000|max_height:1000';
}
答案 1 :(得分:0)
尝试将image
文件输入字段作为数组发送,然后使用数组验证。如果您使用的是Laravel 5.2,则可以使用阵列验证并放弃使用foreach循环来构建规则。
首先,您需要将文件输入设置为数组...
如果您使用的是一个输入字段:
<input type="file" name="image[]" multiple>
您的请求将通过以下方式查看:
array:2 [▼
"_token" => "oX3sTXPDnNiVj9mX20VOHe2xo1Ahx6sWzU4VHZxh"
"image" => array:2 [▼
0 => UploadedFile {#147 ▶}
1 => UploadedFile {#146 ▶}
]
]
然后使用数组验证,您的规则将在验证器中声明,方法是使用asterix符号表示数组字段:
$validator = Validator::make($request->all(),[
'image.*' => 'image|max_width:1000|max_height:1000'
]);
请查看此处的文档以获取更多信息:https://laravel.com/docs/5.2/validation#validating-arrays