file rename while uploading

后端 未结 5 1909
孤独总比滥情好
孤独总比滥情好 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:18

    What about...

    $place_file = "$path/$upload_to/$file_name";     
    
    if (file_exists($place_file)) {
       $place_file = date("mdYHis")."_".$file_name;
    }
    
    if (!move_uploaded_file($tmp, $place_file)) {
       echo "Could not move file";
       exit;
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-16 21:22
    $target = "uploads/$upload_to/$file_name";
    if (file_exists($target)) {
        $pathinfo = pathinfo($target);
        $newName = "$pathinfo[dirname]/" . date('mdYHis') . "_$pathinfo[filename].$pathinfo[extension]";
        rename($target, $newName);
    }
    move_uploaded_file($tmp, $target); 
    

    Beware though: Security threats with uploads.

    0 讨论(0)
  • 2021-01-16 21:34

    how about something like this?

    <?php
    
    $tmp = '/tmp/foo'; // whatever you got out of $_FILES
    $desitnation = '/tmp/bar.xyz'; // wherever you want that file to be saved
    
    if (file_exists($desitnation)) {
      $file = basename($destination)
      $dot = strrpos($file, '.');
    
      // rename existing file to contain its creation time
      // "/temp/bar.xyz" -> "/temp/bar.2012-12-12-12-12-12.xyz"
      $_destination = dirname($destination) . '/'
          . substr($file, 0, $dot + 1)
          . date('Y-m-d-H-i-s', filectime($destination))
          . substr($file, $dot);
    
      rename($destination, $_destination);
    }
    
    move_uploaded_file($tmp, $destination);
    
    0 讨论(0)
  • 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.

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