A Simple File Upload Script

This is a simple file upload script. It is a good starter example which can be customized to meet your needs. This script will allow you to specify an upload directory, allowed file types, max file size and max space allowed to be used in the upload directory. It will even send you an email letting you know there has been an upload to your website. You need to change the variables at the top of the script as needed.

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. See the Tip "Using a custom php.ini file" on this Tips & Scripts page.

- - Start Script Here - -
<?php
$emailaddress = "mail@yourdomain.com";
$this_script = "upload.php";
$home_page = "home.htm";
$uploaddir = "./";
$type = array(".jpg",".gif",".txt"); // enter in all lower case
$maxSize = 100000;
$maxDisplay = $maxSize / 1000;
$maxFileSpace = 50000000;
?>
<html><head></head><body>
<div style="text-align: center; left: 30%; top: 50px; position: absolute; border: 1px black solid; width:400px; height:300px;">
<?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 ($file_name) {
$error = "";
echo "<br>File Name: $file_name<br><br>";
echo "File Size: $file_size<br><br>";
// file size test
if ($file_size == 0 ) $error .= "<font color=red>Invalid file</font><br>";
if ($file_size > $maxSize ) $error .= "<font color=red>Your file exceeds $maxDisplay K.</font><br>";
// file type test
$type_test = strtolower(strstr($file_name, '.'));
if (!in_array($type_test, $type) ) $error .= "<font color=red>Your file is not a valid file type.</font><br>";
// max directory size test
if ($dir = @opendir("$uploaddir")) {
while (($file_select = readdir($dir)) !== false) {
$type_test = strtolower(strstr($file_select, '.'));
if (in_array($type_test,$type)) {
$file_size_accum = filesize("$uploaddir$file_select");
$total_size = $total_size + $file_size_accum;
}
}
closedir($dir);
}
if (($total_size+$file_size) >= $maxFileSpace) $error .= "<font color=red>Total file space limits have been exceeded.</font><br>";
// eliminate bad characters from 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 .= "<font color=red>File already exists.</font><br>";
// if all is valid, do the upload
if ($error == "") {
if (move_uploaded_file($file_tmp_name, "$uploaddir$file_name")) {
chmod("$uploaddir$file_name", 0644);
echo "<font color=green>Your file was successfully uploaded!</font>";
mail($emailaddress, "You have a file upload" , $file_name, "From: Upload <>");
} else {
echo "<font color=red>Your file could not be uploaded.</font>";
}
}
echo "$error<hr>";
} else {
echo "<br><br><br><br>";
}

?>
Upload a <font color='blue'>
<?php
foreach($type as $print_type) { echo $print_type; }
?>
</font> file to our server<br>
Maximum file size is <?=$maxDisplay?> K
<form action="<?=$this_script?>" method="post" enctype="multipart/form-data">
File: <input type=file name="file" size=30><br>
<input type=submit name="submit" value="Upload File"></form>
<a href="<?=$home_page?>">Return to the Home Page</a>
</div></body></html>
- - End Script Here - -