2012-06-22 137 views
0

我有一個問題在這裏我試着上傳文件文件重命名,同時上傳

第一次是從溫度就其各自的目錄中移動的文件名,

但我再次嘗試OT上傳AA不同的文件使用相同的名稱應該與date_somefilename.csv的 第一次上傳的文件

重命名,並給文件名到原來的狀態

例如文件test.csv,即時上傳了第一次的Wi會上傳到 相應的目錄

test.csv,當我上傳同名test.csv不同的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); 
} 
+0

您正在使用系統調用來移動文件? – BugFinder

+0

嘗試使用函數[rename()](http://php.net/manual/en/function.rename.php)而不是系統'mv'。 – Dador

回答

2

參見代碼中的註釋。

$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函數,返回有關路徑的信息:dirname,basename,extension,filename。

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; 
} 
+0

如果日期前置,文件不會移動到正確的路徑。 – Vincent

0
$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

0

這樣的事情呢?

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

如果文件已經存在,我不會添加日期。相反,我只是在它的最後添加一個數字。把事情簡單化。

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