php check file name exist, rename the file

后端 未结 5 1730
忘了有多久
忘了有多久 2020-12-17 04:07

How do I check if file name exists, rename the file?

for example, I upload a image 1086_002.jpg if the file exists, rename the file as 1086_0021.

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 04:41

    You can change your if statement to a while loop:

    $newpath = $fullpath;
    while(file_exists($newpath)) {
        $newpieces = explode(".", $fullpath);
        $frontpath = str_replace('.'.end($newpieces),'',$fullpath);
        $newpath = $frontpath.'1.'.end($newpieces);
    }
    
    file_put_contents($newpath, file_get_contents($_POST['upload']));
    

提交回复
热议问题