views:

85

answers:

2

Here is the code I have, and I'm wondering what I'm doing wrong that it doesn't display the name.

<form action = "self.php" method="post" enctype="multipart/form-data">
<input type="file" name="imageURL[]" id="imageURL" multiple="" />
<input type="submit" value="submit" name="submit" />
</form>

And the processing info that isn't working:

foreach ($_FILES['imageURL'] as $files[]) { 
    echo $files['file'];
}

Edit:

When changing my foreach loop to:

foreach ($_FILES['imageURL'] as $file) { 
    echo $file['name'];
}

Still nothing prints out.

However, when I do something like this:

foreach ($_FILES['imageURL']['name'] as $filename) { 
    echo $filename;
}

The filename does print. I don't know what that implies though.



SOLVED UPDATE:

As linked to by John Conde, the array interlace structure is different when uploading multiple files than when uploading a single file. To use foreach, we must restructure the array.

$files=array();
$fdata=$_FILES['imageURL'];
if(is_array($fdata['name'])){
for($i=0;$i<count($fdata['name']);++$i){
        $files[]=array(
    'name'    =>$fdata['name'][$i],
    'type'  => $fdata['type'][$i],
    'tmp_name'=>$fdata['tmp_name'][$i],
    'error' => $fdata['error'][$i], 
    'size'  => $fdata['size'][$i]  
    );
    }
}else $files[]=$fdata;

NOW we can use foreach to loop:

foreach ($files as $file) { 
    echo $file['name'];
}
+4  A: 

Try

foreach ($_FILES['imageURL'] as $file) { 
    echo $file['name'];
}

UPDATE:

Google found this tutorial which may help you

John Conde
Or `print_r($_FILES);` if the structure is unclear.
Pekka
Does not seem to work, nothing is echo'd.
kylex
@John Conde's UPDATE: brilliant! that was the issue. The array structure is fundementally different which prevents foreach from looping over the files properly. What a horrible way to do that, I'm surprised there isn't a pre-built function in PHP to take care of this somehow...
kylex
@kylex: HTML5 is still in the design stages; PHP probably hasn't added direct support for it due to this.
R. Bemrose
A: 

Maybe I'm wrong, but wouldn't setting multiple="" turn multiple uploads off? Just use multiple by itself, as shown in the HTML5 spec or, for XHTML compatibility, multiple="multiple":

<input type="file" name="imageURL[]" id="imageURL" multiple />
R. Bemrose
A good thought, but not the issue. In fact the Firefox hacks section shows multiple as multiple=""
kylex