Page 1 of 1

Problems using the empty( ) function with $_FILES

Posted: Sun Dec 16, 2007 3:13 am
by mcccy005
Hi there,

I am trying to upload multiple files to my web server, but am having problems trying to check if each of the elements in the array have a file uploaded to them.

Here's the HTML I use to create the form:

Code: Select all

<input type="hidden" name="MAX_FILE_SIZE" value="100000">
Upload Images: <input type="file" name="uploaded_files[]">
                          <input type="file" name="uploaded_files[]">
         ...etc.

And here's the code I'm using to try and check the keys in the $_FILES array at the moment (this is part of a function called "is_empty( ) and returns true if the array is empty, or false if it is NOT empty):

Code: Select all

if($this->data_type=="image_file")
{
	if(!empty($_FILES))
	{ 
		foreach($_FILES[$this->field_name] as $key => $name) 
		{
			if(!empty($_FILES[$this->field_name]["name"][$key]))
			{
				return false; //Returns false if the field has a file selected to be uploaded
			}
			else return true;	//Returns true if no file has been selected to be uploaded
		}
	}
	else return true;
}
(Please note, I'm not having any problems with getting files to actually upload, I just need help trying to check the $_FILES array).
Thanks.

Posted: Sun Dec 16, 2007 5:27 am
by vigge89
PHP includes a error segment for each file in $_FILES ($_FILES['input_name']['error']) which you can look up to see if the file is OK.
PHP Manual: Handling file uploads -> Error Messages Explained