file rename while uploading

后端 未结 5 1950
孤独总比滥情好
孤独总比滥情好 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条回答
  •  孤独总比滥情好
    2021-01-16 21:36

    See comments in code.

    $place_file = "$path/$upload_to/$file_name";     
    
    if (!file_exists($place_file)) {
        move_uploaded_file($tmp, $place_file);  
    } else {
        // first rename
        $pathinfo = pathinfo($place_file);
        $todays_date = date("mdYHis");
        $new_filename = $pathinfo['dirname'].DIRECTORY_SEPARATOR.$todays_date.'_'.$pathinfo['basename'];
        rename($place_file, $new_filename)
        // and then move, not vice versa
        move_uploaded_file($tmp, $place_file); 
    } 
    

    DIRECTORY_SEPARATOR is php constant. Value is '/' or '\', depending of operation system.

    pathinfo() is php function, that return information about path: dirname, basename, extension, filename.

提交回复
热议问题