Validating multiple files in array
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Validating Multiple Files in Array: A Comprehensive Solution for Laravel Developers
Introduction:
Ensuring the integrity of file uploads is essential when handling user-submitted data. This blog post will provide a comprehensive answer to validating multiple files in an array, focusing on Laravel framework. We'll explore best practices to ensure that all submitted files are of the correct type and within the specified size limit.
Body:
1. Inspect Your Code for Errors
Firstly, closely examine your Laravel project code to understand where file validation is being applied. The snippet provided above checks the array object 'files' rather than its contents. To ensure that all files in the uploaded array are checked, consider adjusting the syntax as follows:
public function fileUpload(Request $request)
{
foreach($request->files as $file){
$validator = Validator::make([
'file' => $file
], [
'file' => 'required|mimes:jpeg,jpg,png',
]);
}
}
The above code example iterates through each uploaded file and creates a new validator instance for each one. This approach ensures that validation is applied to every file in the array individually. If you need more flexibility or want to validate multiple files using different criteria, consider creating separate validators for each category of files.
2. Use File Information for Size Checks
Sometimes, you might also need to ensure that uploaded files are within a certain size limit (for example, 2048kb). To do this, use the getClientOriginalSize() method defined in the Illuminate\Http\UploadedFile class provided by Laravel. You can incorporate it into your validation code like so:
public function fileUpload(Request $request)
{
foreach($request->files as $file){
$validator = Validator::make([
'file' => $file,
'size' => [$file, 'max:2048000',],
], [
'file' => 'required|mimes:jpeg,jpg,png',
]);
}
}
Here, we added a new entry to the validator array for the file size. Laravel provides predefined validation rules such as max for specifying maximum file sizes in kilobytes. In this case, we use 'max:2048000' to ensure that every uploaded file is less than 2 megabytes (2048kb).
3. Implement Custom Validation Rules
In some cases, you might need to validate multiple files against custom criteria. For example, you could create a custom rule called 'check_file_size', which takes the max size limit as an argument and checks whether the uploaded file is within this limit:
public function check_file_size($attribute, $value, $parameters)
{
if ($value->getClientOriginalSize() > intval($parameters[0])) {
return false;
}
return true;
}
To use this custom rule in your validation code, define it as a closure or function within the Validator::make method:
public function fileUpload(Request $request)
{
foreach($request->files as $file){
$validator = Validator::make([
'file' => $file,
], [
'file' => ['required', 'mimes:jpeg,jpg,png', new check_file_size(2048000)],
]);
}
}
In the above example, we create a custom rule 'check_file_size' with parameter 2048000 for the max size. The validation then uses this custom rule to ensure that all files are within the specified size limit while retaining the initial mime type check.
Conclusion:
This blog post provides a comprehensive answer on validating multiple files in an array, focusing on the Laravel framework. We've discussed various approaches and best practices for handling file validation, from inspecting your code to using custom validation rules. By implementing these techniques, you can guarantee that your application handles user-submitted data securely and effectively. Remember to always validate every file in an array to ensure a robust implementation.