bluileo Posted February 11, 2009 Share Posted February 11, 2009 Hi People I have managed to get my hands on this script and its perfect but unfortunately it will not let me upload a file with the same name as a file that already exists. Unfortunately I am a complete newbie with php and although I am learning slowly this seems to be beyond my reach. Can someone please tell me what I need to remove / adjust to be able to upload a file with the same name and for that to overwrite the original The source code is below, any and all help greatly appreciated <?php /* Simple Upload manager Script Version 0.4, copyright RRWH.com 2004 This script is distributed under the licence conditions on the website http://rrwh.com/scripts.php This script is a simple interface to allow you to upload files to a configured directory on your server. It will additionally automatically create sub-directories if you want it to. Additionally, It will let you do a directory listing of the base directory or any specified sub-directory. You only need to modify the $password and $dir variable and ensure that the directory exists on the server and the permission is set to 777 You may want to modify the $types if you wish to allow other file types to be uploaded - Be careful not to allow dangerous files to be uploaded to your server. */ $dir = "./pdffiles/"; //Change this to the correct dir RELATIVE TO WHERE THIS SCRIPT IS, or /full/path/ //MIME types to allow, Gif, jpeg, zip ::Edit this to your liking $types = array("application/pdf"); // Nothing to edit below here. //Function to do a directory listing function scandir($dirstr) { echo "<pre>\n"; passthru("ls -l -F $dirstr 2>&1 "); echo "</pre>\n"; } //Check to determine if the submit button has been pressed if((isset($_POST['submit'])) ){ //Shorten Variables $tmp_name = $_FILES['upload']['tmp_name']; $new_name = $_FILES['upload']['name']; $path = $_POST['subdir']; $fullpath = "$dir$path/"; $fullpath = str_replace("..", "", str_replace("\.", "", str_replace("//", "/", $fullpath))); $clean_name = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($new_name) ) ) ); // lets see if we are uploading a file or doing a dir listing if(isset($_POST['Dir'])){ echo "Directory listing for $fullpath\n"; scandir("$fullpath"); }else{ //Check MIME Type if ((in_array($_FILES['upload']['type'], $types)) and (!file_exists($fullpath.$clean_name))){ // create a sub-directory if required if (!is_dir($fullpath)){ mkdir("$fullpath", 0755); } //Move file from tmp dir to new location move_uploaded_file($tmp_name,$fullpath . $clean_name); echo "$clean_name of {$_FILES['upload']['size']} bytes was uploaded sucessfully to $fullpath"; }else{ //Print Error Message echo "<small>File <strong><em>{$_FILES['upload']['name']}</em></strong> Was Not Uploaded - bad file type or file already exists</small><br />"; //Debug $name = $_FILES['upload']['name']; $type = $_FILES['upload']['type']; $size = $_FILES['upload']['size']; $tmp = $_FILES['upload']['name']; echo "Name: $name<br />Type: $type<br />Size: $size<br />Tmp: $tmp"; } } } else { echo ''; } ?> Link to comment https://forums.phpfreaks.com/topic/144823-unable-to-upload-file-with-same-name-assitance-required-please-help/ Share on other sites More sharing options...
The Little Guy Posted February 11, 2009 Share Posted February 11, 2009 change this line: if ((in_array($_FILES['upload']['type'], $types)) and (!file_exists($fullpath.$clean_name))){ to this: if ((in_array($_FILES['upload']['type'], $types))){ Link to comment https://forums.phpfreaks.com/topic/144823-unable-to-upload-file-with-same-name-assitance-required-please-help/#findComment-759987 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.