Reputation: 1629
$_FILES array:
HTML:
<input type="file" name="smth[]" id="smth1" />
<input type="file" name="smth[]" id="smth1" />
<input type="file" name="smth[]" id="smth1" />
How can i check if file array is empty? (no files selected).
PHP:
if (CHECK) {
...operating with $_FILES...
}
Thank you for your answers.
Upvotes: 0
Views: 3124
Reputation: 3754
just check for the file's name:
foreach($_FILES as $key => $val){
if(strlen($_FILES[$key]['name']) > 0){
//here we got a file from user
}else{
//no files received
}
}
Upvotes: 0
Reputation: 288100
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...
}
Upvotes: 1
Reputation: 5856
Actually you'd need to iterate through your $_FILES and check for UPLOAD_ERR_NO_FILE in the error-key. See http://php.net/manual/en/features.file-upload.errors.php for more.
Apart from that, there are countless ways of checking if an array is empty! i.e. empty() or count()
Upvotes: 0