PHP Download and overwrite a file with the same name

I am making an application that allows users to upload a file to a directory through PHP.

I am having problems because the dose does not allow me to overwrite files with the same name. For example, I have a text.php file and I upload it, now when I go back and change the contents of the text.php file and I upload it again to the server, I still have the version without changes. However, if I upload another file, it works. So I just can not overwrite the files.

if ($_POST["greg"]=='true'){ // Set local PHP vars from the POST vars sent from our form using the array // of data that the $_FILES global variable contains for this uploaded file $fileName = $_FILES["file1"]["name"]; // The file name $fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder $fileType = $_FILES["file1"]["type"]; // The type of file it is $fileSize = $_FILES["file1"]["size"]; // File size in bytes $fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true // Specific Error Handling if you need to run error checking if (!$fileTmpLoc) { // if file not chosen echo "ERROR: Please browse for a file before clicking the upload button."; exit(); } else if($fileSize > 90000000000000) { // if file is larger than we want to allow echo "ERROR: Your file was larger than 50kb in file size."; unlink($fileTmpLoc); exit(); } else if (!preg_match("/.(doc|docx|xls)$/i", $fileName) ) { // This condition is only if you wish to allow uploading of specific file types echo "ERROR: Your file is not the right format contact the master of the page for clarification."; unlink($fileTmpLoc); exit(); } // Place it into your "uploads" folder mow using the move_uploaded_file() function move_uploaded_file($fileTmpLoc, "documenti/$fileName"); // Check to make sure the uploaded file is in place where you want it if (!file_exists("documenti/$fileName")) { echo "ERROR: File not uploaded<br /><br />"; echo "Check folder permissions on the target uploads folder is 0755 or looser.<br /><br />"; echo "Check that your php.ini settings are set to allow over 2 MB files, they are 2MB by default."; exit(); } // Display things to the page so you can see what is happening for testing purposes echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />"; echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />"; echo "It is a <strong>$fileType</strong> type of file.<br /><br />"; echo "The Error Message output for this upload is: <br />$fileErrorMsg"; } 

How can I change this code so that when I upload a file with the same name, it overwrites the existing file?

+6
source share
4 answers

Try it (put it before downloading the file)

 //checking if file exsists if(file_exists("documenti/$fileName")) unlink("documenti/$fileName"); //Place it into your "uploads" folder mow using the move_uploaded_file() function move_uploaded_file($fileTmpLoc, "documenti/$fileName"); 
+32
source
 if (file_exists("documenti/$fileName")) { unlink("documenti/$fileName"); echo "<font face='Verdana' size='2' >Last Uploaded File has been removed from uploads folder<br>back to uploadform agian and upload your file<br>";// now your file which uploaded before was deleted from uploads folder you can open it and check if it removed or not , so no you should go back to uploadform again and import your file which will uploaded correctly echo "<font face='Verdana' size='2' ><BR><BR><BR><a href='upform.php'>Back to upform</a><BR>"; } 
+2
source

Maybe the script has no right to overwrite? Try changing the directory to 777 and retest. If it works, you can find the desired value.

+1
source

Have you tried to check if the file exists and delete it if it moves the temporary file to permanent storage?

0
source

Source: https://habr.com/ru/post/901107/


All Articles