Renaming duplicate files in a folder with php

前端 未结 1 1187
灰色年华
灰色年华 2021-01-29 08:24

Who can help me to fix the following problem? Here is the issue: in a form POST i made people can upload files. The code below check if in the \"uploads\" folder there another f

1条回答
  •  一向
    一向 (楼主)
    2021-01-29 09:10

    Just change the $FinalFilename:

    $FinalFilename = pathinfo($OriginalFilename, PATHINFO_FILENAME) . '_' . $FileCounter++ . '.' . pathinfo($OriginalFilename, PATHINFO_EXTENSION);
    

    Or (better if you have a lot of files with the same name and often iterate more than once):

    $filename = pathinfo($OriginalFilename, PATHINFO_FILENAME);
    $extension =  pathinfo($OriginalFilename, PATHINFO_EXTENSION);
    while (file_exists( 'uploads/'.$FinalFilename ))
        $FinalFilename = $filename . '_' . $FileCounter++ . '.' . $extension;
    

    0 讨论(0)
提交回复
热议问题