The specified upload directory can either be a directory relative to the location of the script (in the example the directory "./" is the directory where the script is located, "subdirectory/" would be one directory down from the script) or you can use the full Unix path. Be sure to include a trailing slash on the directory.
If you want to upload files larger than 2MB there are changes required to the php.ini file.
Code: Select all
<?php
/*
This script can send an email and/or make an entry in a log file
There are two variables below - one for an email address and one for a log file
Set both variables to the values you want to use
If you do not want either an email or log entry, comment out the respective line
For example, if you do not want an email sent, put a // in front of the $emailAddress line - same for the $logFile line
*/
$logFile = $_SERVER['DOCUMENT_ROOT'].'/upload.log'; // full path to your log file
$emailaddress = "[email protected]";
$home_page = "home.htm"; // used for a link to return
$uploaddir = "./"; // the directory where files are to be uploaded - include the trailing slash
$fileTypeArray = array(".jpg",".gif",".txt"); // enter in all lower case, the script will handle a match with upper case
$maxSize = 100000; // maximum file size that can be uploaded - in bytes
$maxFileSpace = 50000000; // maximum space that can be used by files matching the $fileTypeArray array in the upload directory - in bytes
putenv('TZ=EST5EDT'); // eastern time
// change nothing below this line
$maxDisplay = $maxSize / 1000;
?>
<html><head></head><body>
<div style="text-align: center; margin: 100px auto; border: 1px black solid; width:400px;">
<?php
// print_r($_FILES); // can be used for debugging
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_tmp_name = $_FILES['file']['tmp_name'];
if (!empty($file_name)) {
unset($error);
echo "<br>File Name: $file_name<br><br>";
echo "File Size: $file_size bytes<br><br>";
// file size test
if ($file_size == 0 ) $error .= "<span style='color: red;'>Invalid file</span><br>";
if ($file_size > $maxSize ) $error .= "<span style='color: red;'>Your file exceeds $maxDisplay K.</span><br>";
// file type test
if (!in_array(strtolower(strrchr($file_name,'.')),$fileTypeArray) ) $error .= "<span style='color: red;'>Your file is not a valid file type.</span><br>";
// max directory size test
foreach(scandir($uploaddir) as $file_select) if (in_array(strtolower(strstr($file_select,'.')),$fileTypeArray)) $total_size = $total_size + filesize($uploaddir.$file_select);
if (($total_size + $file_size) >= $maxFileSpace) $error .= "<span style='color: red;'>Total file space limits have been exceeded.</span><br>";
// scrub characters in the file name
$file_name = stripslashes($file_name);
$file_name = preg_replace("#[ ]#","_",$file_name); // change spaces to underscore
$file_name = preg_replace('#[^()\.\-,\w]#','_',$file_name); //only parenthesis, underscore, letters, numbers, comma, hyphen, period - others to underscore
$file_name = preg_replace('#(_)+#','_',$file_name); //eliminate duplicate underscore
// check for file already exists
if (file_exists($uploaddir.$file_name)) $error .= "<span style='color: red;'>File already exists.</span><br>";
// if all is valid, do the upload
if (empty($error)) {
if (move_uploaded_file($file_tmp_name,$uploaddir.$file_name)) {
chmod($uploaddir.$file_name,0644);
echo "<span style='color: green;'>Your file was successfully uploaded!</span>";
if (isset($emailAddress)) {
$message = $file_name . " was uploaded by".$_SERVER['REMOTE_ADDR']."at".date('Y-m-d H:i:s');
mail($emailaddress,"You have a file upload",$message,"From: Website <>");
}
if (isset($logFile)) {
$logData = $file_name."||".$_SERVER['REMOTE_ADDR']."||".date('Y-m-d H:i:s')."\r\n";
@file_put_contents($logFile,$logData,FILE_APPEND|LOCK_EX);
}
} else {
echo "<span style='color: red;'>Your file could not be uploaded.</span>";
}
}
echo "$error<hr>";
}
?>
<p>Upload a <span style="color: blue;">
<?php
foreach($fileTypeArray as $fileType) echo $fileType;
?>
</span> file to our server<br>
Maximum file size is <?php echo $maxDisplay; ?>K</p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
File: <input type="file" name="file" style="width: 250px;"><br>
<input type="submit" name="submit" value="Upload File"></form>
<a href="<?php echo $home_page; ?>">Return to the Home Page</a>
</div></body></html>