Empty form showing a file was uploaded

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Empty form showing a file was uploaded

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Empty form showing a file was uploaded

Post 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'));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: Empty form showing a file was uploaded

Post 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.
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: Empty form showing a file was uploaded

Post 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. :)
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: Empty form showing a file was uploaded

Post 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!
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: Empty form showing a file was uploaded

Post by katierosy »

In addtion to the count you may check if $_FILES['userfile']['name']!='' like this to help on this
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: Empty form showing a file was uploaded

Post by mickyjune26 »

Thanks for the tip! That does sound like a good idea.
Post Reply