1

I have following fields in HTMl form,

<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>

When I submit the form then I get images[] array in PHP. I am using Laravel framework for form validation. I have been using validator and rules. But I want to apply rule of required and max size to each image from the above array. Thanks.

1

1 Answer 1

2

Here is the example code of my image validations:

// Handle upload(s) with input name "files[]" (array) or "files" (single file upload)

if (Input::hasFile('files')) {
    $all_uploads = Input::file('files');

    // Make sure it really is an array
    if (!is_array($all_uploads)) {
        $all_uploads = array($all_uploads);
    }

    $error_messages = array();

    // Loop through all uploaded files
    foreach ($all_uploads as $upload) {
        // Ignore array member if it's not an UploadedFile object, just to be extra safe
        if (!is_a($upload, 'Symfony\Component\HttpFoundation\File\UploadedFile')) {
        continue;
        }

         $validator = Validator::make(
            array('file' => $upload),
            array('file' => 'required|mimes:jpeg,png|image|max:1000')
         );

        if ($validator->passes()) {
            // Do something
        } else {
            // Collect error messages
            $error_messages[] = 'File "' . $upload->getClientOriginalName() . '":' . $validator->messages()->first('file');
        }
     }

    // Redirect, return JSON, whatever...
    return $error_messages;
} else {
    // No files have been uploaded
}
Sign up to request clarification or add additional context in comments.

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.