Counting uploaded photos

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
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Counting uploaded photos

Post by rick.emmet »

Hi Everyone,
I've been studying the topics at this site and others on ways to count uploaded images; and what I've tried hasn't worked. I have seen this question posed and answered many times and most of the answers are something akin to this:

Code: Select all

 $count = count($_FILES['userfile'] ['name']);
	echo "the number of photos is ".$count;
The explanation usually states that "count($_FILES['userfile'])" will simply count the number of elements in the array and return 5. But including ['name'] will return the number of files (images) uploaded. I will allow up to five photos to be uploaded (less is OK too) and want to count them in order to iterate through the proper number of loops to filter and process the photos. I've tested both of the above and get 5 (elements) when I count the ['userfile']; but get 0 (zero) when I test "['userfile'] ['name']". Here's the html code:

Code: Select all

<!-- html here to input form data -->
<label></label><input type="hidden" name="MAX_FILE_SIZE" value="500000000" /><br />
<label for="userfile">Upload photo 1</label>
<input type="file" name="userfile1" id="userfile1" /><br />
<label for="userfile">Upload photo 2</label>
<input type="file" name="userfile2" id="userfile2" /><br />
<label for="userfile">Upload photo 3</label>
<input type="file" name="userfile3" id="userfile3" /><br />
<label for="userfile">Upload photo 4</label>
<input type="file" name="userfile4" id="userfile4" /><br />
<label for="userfile">Upload photo 5</label>
<input type="file" name="userfile5" id="userfile5" /><br /><br />	
<label></label><input type="submit" name="userfile" value="Send Ad" />
</form>
I've been testing the uploads with two files (jpg) and have tried to come up with an alternative that can iterate through the file arrays and determine how many photos I have. The code I've been testing is as follows:

Code: Select all

$num = 1;
	$count = 0;
	while ($num <=5)	{
 	foreach ($_FILES['userfile'.$num] as $upload){	
   		if ($upload ['error'] != 4){		 
 	}
	$count ++;
  }
  $num++;
}
	echo "the number of photos is ".$count;
I'm using "['error'] !=4" because some times people won't have 5 photos to upload. The result I get is 25 or 5 (files) x 5 (elements) for a total of 25. Does anyone know the proper way of doing this? Thanks for you input!
Cheers,
Rick
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Counting uploaded photos

Post by Christopher »

See this page: http://us3.php.net/manual/en/features.f ... ltiple.php

It would probably be best to check each with is_uploaded_file().

Code: Select all

	$count = 0;
 	foreach ($_FILES['userfile']['tmp_name'] as $key => $tmp_name){	
   		if (is_uploaded_file($tmp_name)){		 
			$count ++;
 		}
	}
	echo "the number of photos is ".$count;
Make your form like this:

Code: Select all

<input type="file" name="userfile[]" id="userfile0" /><br />
<input type="file" name="userfile[]" id="userfile1" /><br />
(#10850)
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

Re: Counting uploaded photos

Post by rick.emmet »

Hi Christopher,
That worked, thank you for taking the time to help me with that one! I much appreciated it.
Cheers,
Rick
Post Reply