...

View Full Version : Help with multiple file upload form



kan3xiao
10-22-2012, 07:27 PM
Hi all,

I came across a pretty good file upload script that I have working perfectly with only one upload box in a form. If I want to have 2 upload boxes in one form, how would I have to adjust the variables in the script below to get it to work.



$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.jpg','.gif','.png', '.pdf');

if (in_array($file_ext,$allowed_file_types) && ($filesize < 1024000)) {
// rename file
$newfilename = md5($file_basename) . $file_ext;
if (file_exists("images/" . $newfilename)) {
// file already exists error
$error[] = "You have already uploaded this file.";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . $newfilename);
}
} elseif (empty($file_basename)) {
// file selection error
$error[] = "Please select a file to upload.";
} elseif ($filesize > 200000) {
// file size error
$error[] = "The file you are trying to upload is too large.";
} else {
// file type error
$error[] = "Only these file types are allowed for upload: " . implode(', ',$allowed_file_types);
unlink($_FILES["file"]["tmp_name"]);
}


and the form in the HTML:



<form method="post" action="upload.php" id="uploadform" enctype="multipart/form-data">
<input type="file" name="file" id="file" size="50">
<input name="upload" type="submit" value="Upload Files">
</form>


I know I have to make another input box, so it would look something like this:



<form method="post" action="upload.php" id="uploadform" enctype="multipart/form-data">
<input type="file" name="file" id="file" size="50">
<input type="file" name="file2" id="file2" size="50">
<input name="upload" type="submit" value="Upload Files">
</form>


But then how would I duplicate and modify the PHP code above? I tried just duplicating the whole code and putting 2s after all the variables but of course that didn't work. Anyone have any ideas? :confused:

abduraooft
10-23-2012, 12:54 PM
Try
<input type="file" name="file[]" id="file" size="50">
<input type="file" name="file[]" id="file2" size="50">


foreach($_FILES as $key => $value){
$filename = $value["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $value["size"];

.............

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum