dongtiannai0654 2012-06-22 11:10
浏览 22
已采纳

上传时重命名文件

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 aa different file with the same name it should rename the first time uploaded file

with date_somefilename.csv and give the filename to its original state

for example a file test.csv ,im uploading it for first time it will upload to corresponding directory as

test.csv,when i upload a different csv file with same name test.csv

I need to get the

test.csv (latest uploaded file)

06222012130209_test.csv(First time uploaded file)

The code is below

$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); 
} 
  • 写回答

5条回答 默认 最新

  • duanmianhong4893 2012-06-22 11:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码