views:

38

answers:

2

I am trying to load multiple files using HTML5. This is my code I found on some site. In the PHP code it doesn't recognize it as an array.

Am I doing something wrong? Can someone show me a working solution?

Thanks.

index.html

<form action='save.php' method='post' enctype='multipart/form-data'>
<input name="uploads" type="file" multiple="multiple" />
<input type='submit' value="Upload File">
</form>

save.php

function GetFiles() {
        $files = array();
        $fdata = $_FILES["uploads"];
        if (is_array($fdata["name"])) {//This is the problem
                for ($i = 0; $i < count($fdata['name']); ++$i) {
                        $files[] = array(
                            'name' => $fdata['name'][$i],
                            'tmp_name' => $fdata['tmp_name'][$i],
                        );
                }
        } else {
                $files[] = $fdata;
        }

        foreach ($files as $file) {
                // uploaded location of file is $file['tmp_name']
                // original filename of file is $file['file']
        }
}
+2  A: 

form variables that are arrays must be named with a [], otherwise PHP doesn't see it as an array. So:

<form action='save.php' method='post' enctype='multipart/form-data'> 
<input name="uploads[]" type="file" multiple="multiple" /> 
<input type='submit' value="Upload File"> 
</form> 

should do it.

dnagirl
+1  A: 

You need to make some sort of array of the name:

<input name="uploads[]" type="file" multiple="multiple" />

Just like you used to do when using checkboxes.

Harmen
thanks! That did the trick. :)
Randall Flagg