Laravel Validate Array of Files Total Allowable Upload Size
I think the best way to validate the total size is by adding a custom validation rule, here is how to do that:
In your controller:
$validatedData = $request->validate([
'videos' => 'array|max_uploaded_file_size:3000',
'videos.*' => 'present|file|mimetypes:video/mp4,video/ogg',
]);
Register custom validation in AppServiceProvider.php
public function boot() {
Validator::extend('max_uploaded_file_size', function ($attribute, $value, $parameters, $validator) {
$total_size = array_reduce($value, function ( $sum, $item ) {
// each item is UploadedFile Object
$sum += filesize($item->path());
return $sum;
});
// $parameters[0] in kilobytes
return $total_size < $parameters[0] * 1024;
});
}