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.
PHP Code:
$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:
Code:
<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:
Code:
<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?