$_FILES check if empty

When I use the following to check if files exist, the form still submits as successfull even when no files are added?


if(!$_FILES['photoname']) {
     $error_msg = 'You must upload your portfolio photos';
}

Field formated like so:


while($counter <= $number_of_fields){
     echo '<input name="photoname[]" type="file" id="upload" />';
$counter++;
}

Thanks


if ( $_FILES['whatever']['error'] > UPLOAD_ERR_OK )
  // we have an error!

The rest is an exercise for you.

Thanks I will look into it…

But shouldnt if(!$_FILES[‘photoname’]) work… as its just like checking on post or get for empty fields?

Nope. It’s the same as a text field; when it is submitted but there was no text filled in $_POST[‘myfield’] is still set, but it’s set to an empty string.
$_FILES[‘photoname’] is also set, but there is no file in there. Same thing.


if($_FILES['photoname']['error'] > 0) { 
      $error_msg = 'You must upload a photo';
}	

From what I read, the above should work?

Altho it appears to stop the script continuing even when atleast one file is uploaded…

Maybe the file you’re uploading is too big? Default max size for PHP uploads is 2MB if I remember correctly (plus there is a limit on the web server as well, if that’s lower than PHP’s limit that’s the limit you need to honour).

File sizes are small… kb’s… I tried filling all 5 fields with files… but $error_msg alway populates with error message

Maybe you should read the error messages that is why they are there.
http://www.php.net/manual/en/features.file-upload.errors.php

There are no errors… but I guess the problem is because its an array of files being sent… not a single file…

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 ) 0

In that case you could check


if (array_sum($_FILES['photoname']['error']) > 0) {
    // There was an error 
}