0

I have a file upload input that allows multiple files to be uploaded. Right now, I am trying to find a way to validate those file uploads but not sure how to work it out.

Code Example

<input type="file" class="form-control" name="file-1649657296668-0[]" access="false" multiple="true" id="file-1649657296668-0">

Let's say I uploaded multiple files on this input, how do I iterate and validate each of the files in this file input and ensure that it passes the validation check? The $request variable was unable to check on each array one by one on this file upload input.

3
  • 2
    Does this answer your question? Validating multiple files in array Commented Apr 12, 2022 at 1:33
  • It doesn't unfortunately. I tried the method based on the link you provided, but the validation still fails. @MichaelMano Commented Apr 12, 2022 at 2:54
  • Also @cleopatez you can do front end validation also accept="image/*" just a side note. Commented Apr 12, 2022 at 3:12

2 Answers 2

6

You can use wildcards * for array items.

<input type="file" name="files[]" multiple>
request()->validate([
    'files' => 'required',
    'files.*' => 'mimes:jpeg,jpg,png'
]);
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this as well using the mimes rule, but the validation error popped up even when the file uploaded follows the right extension. @NickSdot
Laravel is checking the mime type in the content of the file. Maybe the files you test aren’t valid?laravel.com/docs/9.x/validation#rule-mimetypes
1

EDIT: Issue solved. Apparently, on the validator check, I missed the correct format of the input file name, since the ID is automatically generated from my formBuilder. Amended my code to follow the correct format.

Input file name example: file-1649657296668-0[]

E.g:

validated = $request->validate([
                       'file-1649657296668-0.*' => 'max:10000|mimes:pdf,xlsx,xls,docx,doc,png,jpg,jpeg',
                    ]);

Make sure to not miss the asterisk for input type files with multiple attributes, or else it won't validate the rest of the files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.