簡體   English   中英

PHP復制文件並使用日期和時間重命名

[英]PHP Copy a file and rename with date and time

如何將文件從Data/Results/result.txt復制並重命名為Data/Results History/2014-11-13-12-00_result.txt 我的代碼無法正常工作。

$path = "Data/Results/"; // Upload directory
        $tpath = "Data/Results History/"; // Upload to History Folder
        $name = "result.txt";
        $source = $path.$name;
        $today = date("d-m-Y");
        $time = date("H-i-s");
        $newname = $today."_".$time."_".$name;
        $dest = $tpath.$name;
        copy($source,$dest);
        $rename = $tpath.$newname;
        rename($dest,$rename);

您的$source$destination似乎完全相同,如果要將文件復制到另一個目標,則該功能將不起作用。 除此之外,還應該使用copy()復制和移動文件。 rename()只是移動文件而不復制它。

無需使用重命名

只是簡單的使用

// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');

索思

試試這個

<?php
$file = $path.'example.txt';
$newfile = $tpath.'example.txt.bak';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

我在您的$ tpath中看到一個空格,您是否嘗試過

 $tpath = "Data/Results_History/";

重命名應該可以,檢查指定的文件權限和路徑是否正確

if(file_exists($dest)){
 rename($dest,$rename);
}else{ 
// File not exists
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM