我這個PHP代碼:重命名一個文件,如果已存在 - PHP上傳系統
<?php
// Check for errors
if($_FILES['file_upload']['error'] > 0){
die('An error ocurred when uploading.');
}
if(!getimagesize($_FILES['file_upload']['tmp_name'])){
die('Please ensure you are uploading an image.');
}
// Check filesize
if($_FILES['file_upload']['size'] > 500000){
die('File uploaded exceeds maximum upload size.');
}
// Check if the file exists
if(file_exists('upload/' . $_FILES['file_upload']['name'])){
die('File with that name already exists.');
}
// Upload file
if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], 'upload/' . $_FILES['file_upload']['name'])){
die('Error uploading file - check destination is writeable.');
}
die('File uploaded successfully.');
?>
,我需要表現得像一個「窗口」的對待現有文件的 - 我的意思是,如果該文件存在,我希望它被改爲文件名後面的數字1。
例如:myfile.jpg已經存在,所以,如果你再次上傳這將是myfile1.jpg,如果myfile1.jpg存在,這將是myfile11.jpg等等...
我該怎麼辦?我嘗試了一些循環,但不幸沒有成功。
[用php的文件夾重命名中的重複文件]中可能重複(http://stackoverflow.com/questions/11068093/renaming-duplicate-files-in- a-folder-with-php) –