file rename while uploading

后端 未结 5 1952
孤独总比滥情好
孤独总比滥情好 2021-01-16 20:34

I have a problem here im trying to upload a file

first time it is moving the filename from temp it its respective directory,

but again i try ot upload the a

5条回答
  •  猫巷女王i
    2021-01-16 21:19

    I would not add a date to the file if it already exists. Instead I would just add a number to the end of it. Keep it simple.

    $counter = 0;
    do {
        // destination path path
        $destination = $path.'/'.$upload_to.'/';
    
        // get extension
        $file_ext = end(explode('.', $file_name));
    
        // add file_name without extension
        if (strlen($file_ext))
            $destination .= substr($file_name, 0, strlen($file_name)-strlen($file_ext)-1);
    
        // add counter
        if ($counter)
            $destination .= '_'.$counter;       
    
        // add extension
        if (strlen($file_ext))
            $destination .= $file_ext;
    
        $counter++;
    while (file_exists($destination));
    
    // move file
    move_uploaded_file($tmp, $destination);
    

提交回复
热议问题