Page 1 of 1

Empty form showing a file was uploaded

Posted: Tue Jun 01, 2010 11:53 am
by mickyjune26
My php processor for a file upload form is showing that I uploaded a file, even when i press submit with a blank form.

Here is the code that is used to determine how many files were uploaded. It counts correctly when I upload 1, 2, 3 or more files. It does not set the count correctly if the form is submitted with 0 files uploaded.

Code: Select all

	
$num_cntmein = count ($_FILES['ufile']['name']);
	$num_mysims = count ($_FILES['usim']['name']);
	echo "number of \"Count Me In\" uploads = ".$num_cntmein."<br>";
	echo "number of \"My Sims\" uploads = ".$num_mysims."<br>";
Any thoughts?

Re: Empty form showing a file was uploaded

Posted: Tue Jun 01, 2010 1:19 pm
by AbraCadaver
The file controls will still be set in the $_FILES array but they will be empty. I would need to see your input names to tell how would be best to count the non empty ones. Probably something like this:

Code: Select all

count(array_filter($_FILES['ufile']['name'], 'trim'));
Or to see if they actually uploaded, then this:

Code: Select all

count(array_filter($_FILES['ufile']['tmp_name'], 'is_uploaded_file'));

Re: Empty form showing a file was uploaded

Posted: Tue Jun 01, 2010 4:30 pm
by mickyjune26
Here are my form fields for the "Count Me In!" file upload field.

<input name="ufile[]" type="file" id="ufile[]" size="25" />

I'm using an array because I have a javascript-based "Add More" to dynamically create more upload fields.

Does this answer your question?

I'm going to do some research on the array_filter command.

Re: Empty form showing a file was uploaded

Posted: Tue Jun 01, 2010 6:35 pm
by mickyjune26
This code does the trick:

Code: Select all

count(array_filter($_FILES['ufile']['name'], 'trim'));
Now I just need to learn the trim command so I understand what is going on. :)

Re: Empty form showing a file was uploaded

Posted: Tue Jun 01, 2010 6:37 pm
by mickyjune26
oh, i get it now. The array had a null 0 character that was being counted by count. Using trim cut out the null so it was truly empty. cool!

Re: Empty form showing a file was uploaded

Posted: Wed Jun 02, 2010 9:37 am
by katierosy
In addtion to the count you may check if $_FILES['userfile']['name']!='' like this to help on this

Re: Empty form showing a file was uploaded

Posted: Thu Jun 03, 2010 9:45 am
by mickyjune26
Thanks for the tip! That does sound like a good idea.