Please take a look at the following code, everything seems to be working fine apart from the Overwrite feature...it is not giving the overwrite error
<?php
//connection
include('../connection/mysql_connect.php');
//***OTHER SETTINGS
//Change this to the name of the directory you want files to be uploaded to. Leave a trailing slash
$upload_dir = "files/";
//Change this if set to 'yes', files will be overwritten if they have the same name, if no than an error will display.
$overwrite = "no";
//Change this to what you want the maximum allowed upload file size to be.
$maximum_size = "2097152"; // 2097152 = 2 MB
//Scroll down to add extensions
//***DO NOT EDIT UNDER THIS UNTILL YOU SEE A COMMENT
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$upload_url = $url_dir."/".$upload_dir;
//IF NOT MAIN DIRECTORY (/public_html/), WILL NEED CHANGING
$path_to_folder = $_SERVER["DOCUMENT_ROOT"]."/"."members/".$upload_dir;
if (!is_dir("$upload_dir")) {
die ("<center><b>The directory <strong>$upload_dir</strong> does not exist.</center></b>");
}
if ($_FILES['userfile']) {
$message = do_upload($upload_dir, $upload_url);
}
function getFileExt($strFileName){
// Add extensions as you wish
$arrayAllowedExtensions = array("pdf","doc");
$arrayFile = explode(".", $strFileName);
$strExtension = $arrayFile[count($arrayFile)-1];
if(count(array_keys($arrayAllowedExtensions,strtolower($strExtension))))
return true;
else
return false;
}
function do_upload($upload_dir, $upload_url) {
global $overwrite;
global $path_to_folder;
global $maximum_size;
global $file_url;
global $error;
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_type = $_FILES['userfile']['type'];
$file_size = $_FILES['userfile']['size'];
$result = $_FILES['userfile']['error'];
$file_url = $upload_url.$file_name;
$file_path = $upload_dir.$file_name;
if ($file_name == "") {
$message = "<center><strong>No file to upload specified. Go back and try again.</center></strong>";
$error = 1;
return $message;
}
elseif ($overwrite == "no" AND file_exists($path_to_folder.$_FILES['userfile']['name'])) {
die ("<center><b>That file name exists. Please pick another filename and try uploading again.</center></b>");
}
elseif ($file_size > $maximum_size) {
$message = "<center><strong>The file size is over 2 MB.</center></strong>";
$error = 1;
return $message;
}
elseif (!getFileExt($_FILES['userfile']['name'])) {
$message = "<center><strong>Sorry, you can not upload those kinds of files.</center></strong>" ;
$error = 1;
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
$message = "$file_name";
return $message;
}
if ($uploadfile == "1" AND $error != "1") {
$doc_name=$_POST['doc_name'];
$doc_type=$_POST['doc_type'];
$db_url = dirname($_SERVER['PHP_SELF'])."/".$upload_dir.$message;
//Add information to the DB
@mysql_query("insert into docs values(null, '$doc_type', '$doc_name', '$message', '$db_url', now(), 'N')");
//Success Notification
echo "<center><strong>Sucess, file uploaded! Added into DB: $db_url</center></strong>";
//Close Page
exit;
}
//If there is an error, display information and exit the page.
if ($uploadfile == "1") {
echo "$message";
exit;
}
?>
<p align="center"><strong>Upload a File to Replace an Image</strong></p>
<p>Use this form to upload a file that is referenced in a record. Be sure to edit the record to reference the new image file.</p>
<p> </p>
<form name="upload" ENCTYPE="multipart/form-data" method="post" action="">
<div align="center">
<p>Title:
<input type="text" name="doc_name">
</p>
<p>Select File Type:
<select name="doc_type">
<option value="newsletter" selected>Newsletter</option>
<option value="Board Meeting">Board Meeting</option>
</select>
</p>
<p><strong>Select File </strong>
<input name="userfile" type="file">
<input type="hidden" name="uploadfile" value="1">
</p>
<p>
<input type="submit" name="upload" value="Upload">
</p>
</div>
</form>