Log in

View Full Version : Multiple File Uploader



Titan85
03-23-2007, 01:57 AM
Hello, I am trying to make a script that will upload multiple files. I got it to show multiple fields and all, but when it uploads, it only uploads one of the files. Here is the code:
<?php

switch(@$_GET['step']) {
default:
$path = basename(__FILE__)."?step=uploadfiles";
echo '
<h3>Uploader</h3>

<form action="'.$path.'" method="post" enctype="multipart/form-data">
Number of fields: <input type="text" name="number" />

<input type="submit" value="Send" />
</form>
';
break;


###### Upload Files Case ######

case 'uploadfiles':
$total = $_POST['number'];

echo "
<form action=".basename(__FILE__)."?step=upload"." method=\"post\" enctype=\"multipart/form-data\">
Folder Name: <input type=\"text\" name=\"dir\" />
<br />
Files: <br />
";

$i = 0;
while($i != $total) {
echo "<input type=\"file\" name=\"files[]\" />
<br />";
$i++;
}

// End form
echo '
<input type="submit" value="send" />
</form>
';
break;


###### Upload Case ######

case 'upload':

echo '
<h3>Uploader</h3>';

foreach($_FILES['files']['error'] as $key) {
$dir = $_POST['dir'];
if(!is_dir($dir)) {
mkdir($dir);

if(!is_dir($dir)) {
die('Can\'t Create Directory');
}
}

if(is_dir($dir)) {
$tmp_name = $_FILES['files']['tmp_name'][$key];
$name = $_FILES['files']['name'][$key];
move_uploaded_file($tmp_name, $dir."/".$name);

echo "
{$_POST['dir']}/$name - Uploaded Successfully
<br />";
}
}
break;

}

?>I believe the error is near the end around the foreach statement, but can't figure out what exactly it is. I appreciate any help. Thanks

thetestingsite
03-24-2007, 05:32 PM
Sorry for the late reply, but here is your code with the modified parts in it.



<?php

switch(@$_GET['step']) {
default:
$path = basename(__FILE__)."?step=uploadfiles";
echo '
<h3>Uploader</h3>

<form action="'.$path.'" method="post" enctype="multipart/form-data">
Number of fields: <input type="text" name="number" />

<input type="submit" value="Send" />
</form>
';
break;


###### Upload Files Case ######

case 'uploadfiles':
$total = $_POST['number'];

echo "
<form action=".basename(__FILE__)."?step=upload"." method=\"post\" enctype=\"multipart/form-data\">
Folder Name: <input type=\"text\" name=\"dir\" />
<br />
Files: <br />
";

$i = 0;
while($i != $total) {
echo "<input type=\"file\" name=\"files[]\" />
<br />";
$i++;
}

// End form
echo '
<input type="submit" value="send" />
</form>
';
break;


###### Upload Case ######

case 'upload':

echo '
<h3>Uploader</h3>';

foreach($_FILES['files']['name'] as $k => $v) {

$dir = $_POST['dir'];
if(!is_dir($dir)) {
mkdir($dir);

if(!is_dir($dir)) {
die('Can\'t Create Directory');
}
}

if(is_dir($dir)) {
$tmp_name = $_FILES['files']['tmp_name'][$k];
$name = $_FILES['files']['name'][$k];
move_uploaded_file($tmp_name, $dir."/".$name);

echo "
{$_POST['dir']}/$name - Uploaded Successfully
<br />";
}

}
break;

}

?>


Notice the parts in red. Your script was simply uploading one file because the key was getting 0. After changing the first red line, you now get the correct key value (0,1,2, etc) as well as the name of the file. I have tested this script and it works (I got up to 10 files in one upload on one test).

Hope this helps.

Titan85
03-25-2007, 12:19 AM
Ok, I changed it and it works great. So how come it needs to be "$k => $v"? Thanks for the help.

thetestingsite
03-25-2007, 12:22 AM
$k for "key", $v for "value". Before, all it was getting was the value. So I changed to items. The first was $_FILES['files']['error'] (I changed error to name), everytime the script ran this section of code, all of the values (or keys as you had defined it) were zeros. This is why it would only upload the first file and nothing else. I changed this to name, and got the key and value of the $_FILES['files'] array. After that, the key got assigned 0,1,2,... and the value was the name of each file being uploaded.

Hope this helps.