_Aerospace_Eng_
04-28-2006, 01:43 AM
I recently code this for a user on the forums. Basically its a file upload script that will email a person depending on which directory was chosen. I think I've put in sufficient error checking. Once the file is uploaded successfully it redirects the user to a page and outputs that the file was uploaded to so and so directory. If any one sees any problems with it let me know please. One thing you should know if you use this script is that you should NOT allow it to be viewable to the public because you server could be filled up with junk. This should be used in a members only type area on your site. With that said here is the code. I commented it pretty well so it should be easy to follow. The directories you are uploading to must be chmodded to 777 for this script to work properly.
uploader.php
<?php
$error = ""; // Set a variable that will be used for errors
$sendTo = ""; // Set a variable that will be used for emailing
if(isset($_POST['upload']) && $_POST['upload'] == 'Upload File') // Form is submitted
{
$whereto = $_POST['where']; // Gets post value from select menu
$whatfile = $_FILES['uploadedfile']['name']; // Gets file value from file upload input
$subject = "File uploaded to ". $whereto ." directory"; // This is the subject that will appear in the email
$from = "Upload form <noreply@yourdomain.com>";
if(empty($whereto)) // Checks to see if $whereto is empty, if so echo error
{
$error = "You need to choose a directory.<br />";
}
if($whatfile == NULL) // Checks to see if file input field is empty, if so throw an error
{
$error .= "You need to choose a file.";
}
if(!empty($whereto) && $whatfile != NULL) //if no errors so far then continue uploading
{
$target_path = "$whereto/"; // The directory the file will be placed
/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename($whatfile);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
header("Location: index.php?to=$whereto"); // Directs user back to page of choice
}
else
{
/* if there was a problem then throw an error */
$error .= "There was an error uploading the file, please try again!";
}
if(empty($error))
{
if($whereto == "uploads1") // Change $sentTo depending on the directory chosen
{
$sendTo = "Jim <jim@yourdomain.com>"; // Change this to an email address of your own
}
if($whereto == "uploads2")
{
$sendTo = "Kim <kim@yourdomain.com>"; // Change this to an email address of your own
}
/* The below will be what is shown in the email */
$body = "You have received the following from the web based upload form:\r\n";
$body .= "---------------------------------------------------------------\r\n";
$body .= "Subject: File uploaded to ". $whereto ." directory\r\n";
$body .= "File name: ". $whatfile ."\r\n";
$body .= "Upload directory: ". $whereto ."\r\n";
$body .= "Uploader's IP address: ". $_SERVER['REMOTE_ADDR'] ."\r\n";
$body .= "---------------------------------------------------------------\r\n";
/* headers used to show from field in email client */
$headers = 'From: '.$from."\r\n" .
'Reply-To: '.$from;
/* this actually sends the mail */
mail($sendTo, $subject, $body, $headers);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload</title>
</head>
<body>
<div>
<?php
if(!empty($error))
{
echo $error;
}
?>
</div>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<select name="where">
<option value="">Choose a directory</option>
<option value="uploads1">uploads1</option>
<option value="uploads2">uploads2</option>
</select>
<input type="submit" name="upload" value="Upload File" />
</form>
</body>
</html>
index.php
<?php
$directory = $_GET['to']; // This checks the url getting the value for the to=
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Index Page</title>
</head>
<body>
<?php
if(!empty($directory)) // if to=something then echo that the file was uploaded in so and so directory
{
echo "Your file has been uploaded to the ". $directory ." directory.";
}
?>
</body>
</html>
uploader.php
<?php
$error = ""; // Set a variable that will be used for errors
$sendTo = ""; // Set a variable that will be used for emailing
if(isset($_POST['upload']) && $_POST['upload'] == 'Upload File') // Form is submitted
{
$whereto = $_POST['where']; // Gets post value from select menu
$whatfile = $_FILES['uploadedfile']['name']; // Gets file value from file upload input
$subject = "File uploaded to ". $whereto ." directory"; // This is the subject that will appear in the email
$from = "Upload form <noreply@yourdomain.com>";
if(empty($whereto)) // Checks to see if $whereto is empty, if so echo error
{
$error = "You need to choose a directory.<br />";
}
if($whatfile == NULL) // Checks to see if file input field is empty, if so throw an error
{
$error .= "You need to choose a file.";
}
if(!empty($whereto) && $whatfile != NULL) //if no errors so far then continue uploading
{
$target_path = "$whereto/"; // The directory the file will be placed
/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename($whatfile);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
header("Location: index.php?to=$whereto"); // Directs user back to page of choice
}
else
{
/* if there was a problem then throw an error */
$error .= "There was an error uploading the file, please try again!";
}
if(empty($error))
{
if($whereto == "uploads1") // Change $sentTo depending on the directory chosen
{
$sendTo = "Jim <jim@yourdomain.com>"; // Change this to an email address of your own
}
if($whereto == "uploads2")
{
$sendTo = "Kim <kim@yourdomain.com>"; // Change this to an email address of your own
}
/* The below will be what is shown in the email */
$body = "You have received the following from the web based upload form:\r\n";
$body .= "---------------------------------------------------------------\r\n";
$body .= "Subject: File uploaded to ". $whereto ." directory\r\n";
$body .= "File name: ". $whatfile ."\r\n";
$body .= "Upload directory: ". $whereto ."\r\n";
$body .= "Uploader's IP address: ". $_SERVER['REMOTE_ADDR'] ."\r\n";
$body .= "---------------------------------------------------------------\r\n";
/* headers used to show from field in email client */
$headers = 'From: '.$from."\r\n" .
'Reply-To: '.$from;
/* this actually sends the mail */
mail($sendTo, $subject, $body, $headers);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload</title>
</head>
<body>
<div>
<?php
if(!empty($error))
{
echo $error;
}
?>
</div>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<select name="where">
<option value="">Choose a directory</option>
<option value="uploads1">uploads1</option>
<option value="uploads2">uploads2</option>
</select>
<input type="submit" name="upload" value="Upload File" />
</form>
</body>
</html>
index.php
<?php
$directory = $_GET['to']; // This checks the url getting the value for the to=
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Index Page</title>
</head>
<body>
<?php
if(!empty($directory)) // if to=something then echo that the file was uploaded in so and so directory
{
echo "Your file has been uploaded to the ". $directory ." directory.";
}
?>
</body>
</html>