Tôi đang tạo một ứng dụng cho phép người dùng tải lên tệp trong thư mục qua PHP.Tệp PHP tải lên và ghi đè lên tệp có cùng tên
Tôi gặp sự cố vì nó không cho phép tôi ghi đè lên các tệp có cùng tên. Ví dụ: tôi có một tệp gọi là text.php và tôi tải nó lên, bây giờ khi tôi quay lại và thay đổi nội dung của tệp text.php và tôi tải nó lên máy chủ, tôi vẫn có phiên bản mà không cần chỉnh sửa. Tuy nhiên nếu tôi tải lên một tập tin khác nó hoạt động. Vì vậy, tôi không thể ghi đè lên các tập tin.
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";
}
Làm cách nào để thay đổi mã này để tôi tải lên tệp có cùng tên ghi đè tệp hiện có?
Bạn có chắc chắn nó không bị ghi đè, với mã của bạn tiêu chuẩn hành vi trên máy chủ giống LAMP là tệp sẽ bị ghi đè. SO đảm bảo CHMOD của bạn là 777 –
để tôi làm điều đó trong php.ini – Gunnit
Không, chỉ cần truy cập tệp của bạn bằng FTP và nhấp chuột phải và đặt tốc độ tối đa là 777) –