简体   繁体   中英

checking mutiple file input in php

Guys have a problem don't know why this is not working, i have file input field which can upload multiple images.

this is my form

<form method="post" action="" enctype="multipart/form-data">
<input name="images[]" type="file" multiple="multiple">
<input type="submit" name="test" value="test"/>
</form> 

If i submitted the code without selecting any files, i need to display an error message. Here is my php code

if(!empty($_FILES['images']['name'])){
foreach($_FILES['images']['name'] as $key =>$value){
//other code
}
}else{
echo 'No images have been selected';
}

but this won't work any idea why is that..?

Try this:

$hasUpload = false;
if(isset($_FILES['images']['name'])){
    foreach($_FILES['images']['name'] as $key => $value){
        if(!empty($value)) {
            // some codes here
            $hasUpload = true;
        }
    }
}
if (!$hasUpload) {
    echo 'No images have been selected';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM