自己一直在使用我的PHP上傳腳本,但遇到了阻止覆蓋現有文件的問題,該怎麼做。請要求提示和解釋。如果我的方式來處理上傳是好的,如果不是,請告知並給出提示。在我看來如何防止PHP上傳腳本覆蓋現有文件?
$destination = 'C:/upload_test/';
$max=75200;
if (isset($_POST['upload'])) {
if (isset($_FILES['image']['tmp_name'])) {
$fileTaille= $_FILES['image']['size'];
if ($fileTaille==true) {
if ($fileTaille > $max) {
echo "Your file is too large, select a file smaller than";
exit(include 'form.php');
}
}
else {
echo "No file selected";
exit(include 'form.php');
}
}
$file_type=getimagesize($_FILES['image']['tmp_name']);
if ($file_type==true) {
echo "File is an image - " .$file_type["mime"]." ";
}
else{
echo "Could not get file type";
}
$fileType = exif_imagetype($_FILES['image']['tmp_name']);
$allowed = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
if (!in_array($fileType, $allowed)) {
echo "File type not accepted, Only JPEG file allowed";
exit(include 'form.php');
}
$clean_file = preg_replace("/[^A-Z0-9\.\_-]/i", " ", $_FILES["image"]["name"]);
$fileName = $destination . basename($clean_file);
if (file_exists($fileName)) {
echo "File already exist";
exit(include 'form.php');
}
}
if (isset($_FILES['image']['tmp_name'])) {
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination . $_FILES['image']['name']);
if ($result == true) {
echo "file moved "." ";
}else
{
echo "Could not move filed";
}
$permission = chmod($destination . $clean_file, 0644);
if ($permission==false) {
echo "No permission to the file";
}
else
{
echo "permission given";
}
}
?>
提示:閱讀[在PHP文件上傳手冊頁](http://php.net/manual/en/features.file-upload.php的)我注意到最明顯的是你沒有在任何地方檢查'['error']' – RiggsFolly
你也試圖上傳到一個我認爲在DocumentRoot之外的文件夾。嘗試將其更改爲網站的子文件夾,而不是完全獨立的文件夾,至少在您獲得它的工作之前。 – RiggsFolly
@RiggsFolly是的,我只在我的DocumentRoot之外上傳,僅用於在本地計算機上進行測試.. –