Multiple File Uploader
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 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
Thanks DD, you saved me countless times
Bookmarks