Page 1 of 1

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

Posted: Sun Aug 23, 2009 2:00 am
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

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

Posted: Sat Aug 29, 2009 12:31 pm
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.

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

Posted: Sat Aug 29, 2009 3:08 pm
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'])