0

$_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.

0

4 Answers 4

1
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...
}
Sign up to request clarification or add additional context in comments.

Comments

0

something like

if(isset($_FILES) && count($_FILES) > 0){
...

?

1 Comment

Yes, something like this, only that doesn't work - $FILES is always set.
0

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()

Comments

0

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
  }
}

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.