【问题标题】:file rename while uploading上传时文件重命名
【发布时间】:2012-06-24 16:26:44
【问题描述】:

我在尝试上传文件时遇到问题

第一次将文件名从 temp 移动到其各自的目录,

但我再次尝试上传具有相同名称的不同文件,它应该重命名 第一次上传文件

使用 date_somefilename.csv 并将文件名恢复为原始状态

例如一个文件 test.csv ,我第一次上传它会上传到 对应目录为

test.csv,当我上传另一个同名的 csv 文件 test.csv

我需要得到

test.csv(最新上传的文件)

06222012130209_test.csv(首次上传文件)

代码如下

$place_file = "$path/$upload_to/$file_name";     



if (!file_exists('uploads/'.$upload_to.'/'.$file_name)) 
 {

move_uploaded_file($tmp, $place_file);  


}else{

 move_uploaded_file($tmp, $place_file); 
 $arr1 = explode('.csv',$file_name);
  $todays_date =  date("mdYHis");
   $new_filename = $todays_date.'_'.$arr1[0].'.csv';
  echo  $str_cmd = "mv " . 'uploads/'.$upload_to.'/'.$file_name . " uploads/$upload_to/$new_filename";
   system($str_cmd, $retval); 
} 

【问题讨论】:

  • 您正在使用系统调用来移动文件?
  • 尝试使用函数rename()而不是系统'mv'。

标签: php


【解决方案1】:

怎么样……

$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;
}

【讨论】:

  • 如果前面有日期,文件不会移动到正确的路径。
【解决方案2】:

查看代码中的 cmets。

$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 是 php 常量。值是 '/' 或 '\',取决于操作系统。

pathinfo() 是 php 函数,它返回有关路径的信息:目录名、基名、扩展名、文件名。

【讨论】:

    【解决方案3】:
    $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); 
    

    但请注意:Security threats with uploads

    【讨论】:

      【解决方案4】:

      这样的事情怎么样?

      <?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);
      

      【讨论】:

        【解决方案5】:

        如果文件已经存在,我不会在文件中添加日期。相反,我只会在它的末尾添加一个数字。保持简单。

        $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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-19
          • 1970-01-01
          • 1970-01-01
          • 2014-03-15
          相关资源
          最近更新 更多