PDA

View Full Version : Uploading multiple files using http



SysX
08-18-2005, 08:04 AM
can someone please tell me how to get my multiple upload working properly...

I have this in my file.php:

// Added for PHP with register_globals = off
if ($sourcefile == "") {
$sourcefile = $_FILES['sourcefile']['tmp_name'][0];
$sourcefile_size = $_FILES['sourcefile']['size'][0];
$sourcefile_name = $_FILES['sourcefile']['name'][0];
$sourcefile_type = $_FILES['sourcefile']['type'][0];

$sourcefile = $_FILES['sourcefile']['tmp_name'][1];
$sourcefile_size = $_FILES['sourcefile']['size'][1];
$sourcefile_name = $_FILES['sourcefile']['name'][1];
$sourcefile_type = $_FILES['sourcefile']['type'][1];

$sourcefile = $_FILES['sourcefile']['tmp_name'][2];
$sourcefile_size = $_FILES['sourcefile']['size'][2];
$sourcefile_name = $_FILES['sourcefile']['name'][2];
$sourcefile_type = $_FILES['sourcefile']['type'][2];

$sourcefile = $_FILES['sourcefile']['tmp_name'][3];
$sourcefile_size = $_FILES['sourcefile']['size'][3];
$sourcefile_name = $_FILES['sourcefile']['name'][3];
$sourcefile_type = $_FILES['sourcefile']['type'][3];

}

And this in my upload_page.inc:

<tr>
<td class="body" width="20%">
<input type=file name="sourcefile[0]" class="Text220" size="100"><br />
<input type=file name="sourcefile[1]" class="Text220" size="100"><br />
<input type=file name="sourcefile[2]" class="Text220" size="100"><br />
<input type=file name="sourcefile[3]" class="Text220" size="100"><br />
</td>
</tr>



It used to be this:

// Added for PHP with register_globals = off
if ($sourcefile == "") {
$sourcefile = $_FILES['sourcefile']['tmp_name'];
$sourcefile_size = $_FILES['sourcefile']['size'];
$sourcefile_name = $_FILES['sourcefile']['name'];
$sourcefile_type = $_FILES['sourcefile']['type'];
}

And this:

<tr>
<td class="body" width="20%">
<input type=file name="sourcefile" class="Text220" size="100">
</td>
</tr>


and now it's not working properly?

Twey
08-18-2005, 05:56 PM
All this

$sourcefile = $_FILES['sourcefile']['tmp_name'][0];
$sourcefile_size = $_FILES['sourcefile']['size'][0];
$sourcefile_name = $_FILES['sourcefile']['name'][0];
$sourcefile_type = $_FILES['sourcefile']['type'][0];

$sourcefile = $_FILES['sourcefile']['tmp_name'][1];
$sourcefile_size = $_FILES['sourcefile']['size'][1];
$sourcefile_name = $_FILES['sourcefile']['name'][1];
$sourcefile_type = $_FILES['sourcefile']['type'][1];

$sourcefile = $_FILES['sourcefile']['tmp_name'][2];
$sourcefile_size = $_FILES['sourcefile']['size'][2];
$sourcefile_name = $_FILES['sourcefile']['name'][2];
$sourcefile_type = $_FILES['sourcefile']['type'][2];

$sourcefile = $_FILES['sourcefile']['tmp_name'][3];
$sourcefile_size = $_FILES['sourcefile']['size'][3];
$sourcefile_name = $_FILES['sourcefile']['name'][3];
$sourcefile_type = $_FILES['sourcefile']['type'][3];
is nonsense. You're just overwriting the variable with one array item after another, not copying the whole array to the variable.

In fact, I'm not so sure about putting square brackets in the names. I don't think this will cause it to work as you expect, creating an array. Try renaming the variables to something sensible.

SysX
08-19-2005, 01:31 AM
Ok I'm now using this instead:


$numfiles = count($_FILES['sourcefile']['name']);

for ($i=0; $i<$numfiles; $i++) {
if (!empty($_FILES['sourcefile']['name'][$i])) {
$sourcefile = $_FILES['sourcefile']['tmp_name'][$i];
$sourcefile_size = $_FILES['sourcefile']['size'][$i];
$sourcefile_name = $_FILES['sourcefile']['name'][$i];
$sourcefile_type = $_FILES['sourcefile']['type'][$i];

# process the uploaded file

}
}


but it only uploads the last file in the listing:

<input type=file name="sourcefile[]" class="Text220" size="100">
<input type=file name="sourcefile[]" class="Text220" size="100">
<input type=file name="sourcefile[]" class="Text220" size="100">

Twey
08-19-2005, 08:00 AM
That's still no good. Let me break down that loop for you.

Loop 0:
Set $sourcefile to $_FILES['sourcefile']['tmp_name'][0]

Loop 1:
Set $sourcefile to $_FILES['sourcefile']['tmp_name'][1]

As you see, this overwrites the previous value, so that you are left with only the last file.

You should create more variables, or (better) an array.

if (!empty($_FILES['sourcefile']['name'])) {
$sourcefile = $_FILES['sourcefile']['tmp_name'];
$sourcefile_size = $_FILES['sourcefile']['size'];
$sourcefile_name = $_FILES['sourcefile']['name'];
$sourcefile_type = $_FILES['sourcefile']['type'];

# process the uploaded file

}

This will give you $sourcefile[0], $sourcefile_size[0]. &c.