php PHP文件上传和覆盖同名文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8064618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP File upload and overwrite file with same name
提问by Gunnit
I am making an application that allows users to upload a file in a directory via PHP.
我正在制作一个应用程序,允许用户通过 PHP 上传目录中的文件。
I am having problems because it dose not allow me to overwrite files with the same name. Eg I have a file called text.php and I upload it, now when I go back and change the content of file text.php and I upload it again on the server I still have the version without the edits. However if I upload another file it works. So I just can't overwrite files.
我遇到了问题,因为它不允许我覆盖同名文件。例如,我有一个名为 text.php 的文件并上传了它,现在当我返回并更改文件 text.php 的内容并再次将其上传到服务器上时,我仍然拥有未经编辑的版本。但是,如果我上传另一个文件,它就可以工作。所以我不能覆盖文件。
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?
如何更改此代码,以便在上传同名文件时覆盖现有文件?
回答by Dezigo
Try this (put it before upload a 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");
回答by user1562744
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>";
}
回答by Flo
Maybe the script does not have the rights to overwrite? Try to change the dir to 777 and test again. If it works then, you can figure out the correct value you need
也许脚本没有覆盖的权限?尝试将目录更改为 777 并再次测试。如果它有效,你可以找出你需要的正确值
回答by Shane McIntosh
Have you tried checking if the file exists and deleting it if it does before you move the temporary file to the permanent storage location?
在将临时文件移动到永久存储位置之前,您是否尝试过检查文件是否存在并删除它?