Jump to content

Counting uploaded images


rick.emmet

Recommended Posts

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:

 

       

$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:

 

<!-- 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:

 

$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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.