Search code examples
phpuploadselected

how to check if one or more files are selected for file upload


I am trying to check if one or more files are selected for file upload but i do not succeed in it. This is the form input:

<input class="form-control-file" type="file" name="new_image[]" value="" multiple />

And my php:

if($_FILES["new_image"]["error"] == 0 ) { // if one or more file(s) are selected
    echo 'file selected';

I do not get the echo back...


Solution

  • Have you checked if you gave your form tag enctype="multipart/form-data" attribute to allow file uploads?

    When multiple files are uploaded the error attribute is sent as an array, take a look at below snippet.

    foreach ($_FILES["new_image"]['error'] as $key => $error) {
                if($error === 0){
                    echo 'file selected';
                    break;
                }
            }