Get the correct count of $_FILES

I want to get the correct count of the $_FILES from html upload

<form>
    <input type='file' name='file[]'/>
    <input type='file' name='file[]'/>
    <input type='file' name='file[]'/>
</form>

[php]
$count1 = count($_FILES[‘file’]);

$count2 = count($_FILES);
[/php]

The count() is counting the array within the $_FILES array
($_FILES[‘name’], $_FILES[‘type’], $_FILES[‘size’], $_FILES[‘error’], $_FILES[‘tmp’])

Both of those return 5 when I need it to return 3 (or however many are set)

I’ve googled this but people insist that using count() or sizeof() works

Has anyone else encountered this problem and found a simplish fix?

try

[code]


[/code]

[php]echo count($_FILES[‘file’][‘tmp_name’])[/php]

That also returns 5…

I created a pretty simple solution for this…

[php]
DEFINE(“NUM_OF_ALLOWED_IMAGES”, 6);

$imgCount = 0;
 
for($i = 0; $i < NUM_OF_IMAGES; $i++){
	if($_FILES['image']['size'][$i] > 0){
		$imgCount++;
	}
}

[/php]

That’s not getting the number of images though, you’re just telling it to stop when it hits 5… $imgcount isn’t doing anything. What you could do though is this
[php]
$imgct = array();
for($i = 0; $i < NUM_OF_IMAGES; $i++) {
if($_FILES[‘image’][‘size’][$i] > 0) {
$imgct[] = $i;
$imgCount++;
}
}
echo count($imgct);
[/php]

It’s doing what I want it to do. I’ll comment it out.

[php]
DEFINE(“NUM_OF_ALLOWED_IMAGES”, 6); // This is the maximum number of allowed images

$imgCount = 0;  // Initialize the count of images to zero
 
for($i = 0; $i < NUM_OF_ALLOWED_IMAGES; $i++){ // Run the loop to check for image ( 6 times for this case)
	if($_FILES['image']['size'][$i] > 0){ 
		$imgCount++; // If the size of that image is greater than zero than an image is being uploaded so we add on to the image count
	}
}

[/php]

So if 2 images were being uploaded than the $imgCount would be “2”.

The downside to doing this is the script doesn’t know which inputs they used for the uploads.

Example…

Say you have six allowed files to upload,

<input type='file' name='image[]'/>
<input type='file' name='image[]'/>
<input type='file' name='image[]'/>
<input type='file' name='image[]'/>
<input type='file' name='image[]'/>
<input type='file' name='image[]'/>

Now the user decides to upload only two images, but doesn’t start from the first input…

<input type='file' name='image[]'/>  <!-- They leave this one blank -->
<input type='file' name='image[]'/>  <!-- They put a picture in this one -->
<input type='file' name='image[]'/>  <!-- And this one -->
<input type='file' name='image[]'/>
<input type='file' name='image[]'/>
<input type='file' name='image[]'/>

If you use the for() loop above, the script will automatically just assume that the first two inputs were used. So the “first” image would be uploaded as the second image and the “second” second image wouldn’t be uploaded.

So this script works, in a way. I’m using javascript to hide the extra inputs unless they say they want to upload another image.

Sponsor our Newsletter | Privacy Policy | Terms of Service