How can I use Laravel file validation to a multiple file inside a loop?
Usually in a single file validation, I can just do like
$validator = Validator::make($request->all(),[
'image' => 'image|max_width:1000|max_height:1000'
]);
if($validator->fails()){
//validation fails
}
but now I want to validate multiple files in a single process, like
//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
}
}
Now for the front end side, to elaborate about the process of sending multiple files in one post.
$(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);
}
and so in the back end if I dump the request, I get these
array(2) { [0]=> object(Illuminate\Http\UploadedFile)#214 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(50) "12400457_643171672489204_2452820305362127031_n.jpg" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(18413) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(25) "C:\wamp64\tmp\phpBAE4.tmp" ["fileName":"SplFileInfo":private]=> string(11) "phpBAE4.tmp" } [1]=> object(Illuminate\Http\UploadedFile)#217 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(39) "13296200_702501119889592_55691535_n.jpg" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(95383) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(25) "C:\wamp64\tmp\phpBAE5.tmp" ["fileName":"SplFileInfo":private]=> string(11) "phpBAE5.tmp" } }
any ideas, clues, help please on how to use Laravel file validation on a multiple files inside a loop?
PS: I'm running on Laravel 5.2