Need Help to count input fields---- count($_FILES['file'])

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
kris_reny
Forum Newbie
Posts: 1
Joined: Sun Aug 23, 2009 1:42 am

Need Help to count input fields---- count($_FILES['file'])

Post by kris_reny »

I have the following php script which counts the number of file input fields in myform

Code: Select all

<?php   if(isset($_POST['submit'])) 
{       
$i = 0;
while($i <= count($_FILES['fileupload']))
{
print $i;       
print $_FILES['fileupload']['name'][$i]."<br />";   
$i++;       
}
 
}
?>
The HTML is as follows

Code: Select all

<html><body><form name="upload" method="post" action="" enctype="multipart/form-data">
 <table>
 <tr>
 <td>
 <input type="file" name="fileupload[]">
 
 </td>
 </tr>
 <tr>
 <td>
 <input type="file" name="fileupload[]">
 
 </td>
 </tr>
 <tr>
 <td>
 <input type="file" name="fileupload[]">
 
 </td>
 </tr>
 <tr>
 <td>
 <input type="file" name="fileupload[]">
 
 </td>
 </tr>
 <tr>
 <td>
 <input type="file" name="fileupload[]">
 
 </td>
 </tr>
 <tr>
 <td>
 <input type="file" name="fileupload[]">
 
 </td>
 </tr>
  
 <tr>
 <td>
 <input type="submit" name="submit">
 
 </td>
 </tr>
 </table>
The
count($_FILES['fileupload'])
line in my code returns 5 eventhough I have six or more input fields in the form.
If i select five images and hit submit, then the code returns me the image names of selected five images. But it doesn't return any thing if I select more than five files.
Can any one help me to solve this issue? Thank-you in advance
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Need Help to count input fields---- count($_FILES['file'

Post by McInfo »

If you upload six files, the $_FILES array will look something like this:

Code: Select all

$_FILES => Array
(
    [fileupload] => Array
        (
            [name] => Array
                (
                    [0] => a.png
                    [1] => b.png
                    [2] => c.png
                    [3] => d.png
                    [4] => e.png
                    [5] => f.png
                )
            [type] => Array
                (
                    [0] => image/png
                    [1] => image/png
                    [2] => image/png
                    [3] => image/png
                    [4] => image/png
                    [5] => image/png
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/php9EA1.tmp
                    [1] => /tmp/php9EA2.tmp
                    [2] => /tmp/php9EA3.tmp
                    [3] => /tmp/php9EA4.tmp
                    [4] => /tmp/php9EA5.tmp
                    [5] => /tmp/php9EA6.tmp
                )
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )
            [size] => Array
                (
                    [0] => 1111
                    [1] => 2222
                    [2] => 3333
                    [3] => 4444
                    [4] => 5555
                    [5] => 6666
                )
        )
)
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 7:13 pm, edited 1 time in total.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Need Help to count input fields---- count($_FILES['file'])

Post by Darhazer »

count($_FILES['file']) will always return 5, since those are the 5 keys in the array - name, type, tmp_name, size and error. So you should count($_FILES['file']['name'])
Post Reply