$_FILES 数组:
HTML:
<input type="file" name="smth[]" id="smth1" />
<input type="file" name="smth[]" id="smth1" />
<input type="file" name="smth[]" id="smth1" />
如何检查文件数组是否为空?(未选择文件)。
PHP:
if (CHECK) {
...operating with $_FILES...
}
谢谢您的回答。
function any_uploaded($name) {
foreach ($_FILES[$name]['error'] as $ferror) {
if ($ferror != UPLOAD_ERR_NO_FILE) {
return true;
}
}
return false;
}
if (any_uploaded('smth')) {
// ..operating with $_FILES...
}
就像是
if(isset($_FILES) && count($_FILES) > 0){
...
?
实际上,您需要遍历 $_FILES 并检查错误键中的 UPLOAD_ERR_NO_FILE 。有关更多信息,请参见http://php.net/manual/en/features.file-upload.errors.php。
除此之外,还有无数种方法可以检查数组是否为空!即空()或计数()
只需检查文件名:
foreach($_FILES as $key => $val){
if(strlen($_FILES[$key]['name']) > 0){
//here we got a file from user
}else{
//no files received
}
}