php check file name exist, rename the file

后端 未结 5 1737
忘了有多久
忘了有多久 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:31

    Try something like:

    $fullpath = 'images/1086_002.jpg';
    $additional = '1';
    
    while (file_exists($fullpath)) {
        $info = pathinfo($fullpath);
        $fullpath = $info['dirname'] . '/'
                  . $info['filename'] . $additional
                  . '.' . $info['extension'];
    }
    

提交回复
热议问题