John
John

Reputation: 1629

Check/count if file array is empty in php

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

Answers (4)

Janaka R Rajapaksha
Janaka R Rajapaksha

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

phihag
phihag

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

worenga
worenga

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

Aerik
Aerik

Reputation: 2317

something like

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

?

Upvotes: 0

Related Questions