我正在制作一个应用程序,允许用户通过PHP上传目录中的文件。
我遇到了问题,因为它不允许我覆盖同名文件。 例如,我有一个名为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";
}
如何更改此代码,以便在上传具有相同名称的文件时覆盖现有文件?
答案 0 :(得分:33)
试试这个(在上传文件之前把它放好)
//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");
答案 1 :(得分:2)
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 :(得分:1)
也许脚本没有覆盖的权限?尝试将目录更改为777并再次测试。如果它可以工作,你可以找出你需要的正确值
答案 3 :(得分:0)
您是否尝试检查该文件是否存在,并在将临时文件移至永久存储位置之前将其删除?