我这个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) –