如何检查文件名是否存在,重命名文件?
例如,如果文件存在,我上传图像1086_002.jpg,将文件重命名为1086_0021.jpg并保存,如果存在1086_0021.jpg,则重命名为1086_00211.jpg并保存,如果存在1086_00211.jpg,则重命名为1086_002111.jpg并保存…
这是我的代码,它只能存在1086_002.jpg,将文件重命名为1086_0021.jpg,也许应该做一个foreach,但是如何?
//$fullpath = 'images/1086_002.jpg';
if(file_exists($fullpath)) {
$newpieces = explode(".", $fullpath);
$frontpath = str_replace('.'.end($newpieces),'',$fullpath);
$newpath = $frontpath.'1.'.end($newpieces);
}
file_put_contents($newpath, file_get_contents($_POST['upload']));