簡體   English   中英

為什么PHP named()刪除文件?

[英]Why PHP rename() is deleting files?

我無法確定我的代碼出了什么問題。 這行代碼將刪除文件,而不是重命名文件。

for($i=0;$i<(7-$n);$i++){   
    rename($location."/".($last-$i).".txt", $location."/".($last-$i-$n).".txt");
    rename($location."/".($last-$i).".bmp", $location."/".($last-$i-$n).".bmp");            
}

我已經回顯了源文件名和目標文件名,對我來說它看起來是正確的。 我正在使用unlink()函數執行相同的“變量沙拉”,並且其行為符合預期。 我試圖制作自己的rename()函數:

function myRename($old, $new){
  $content = printFile($old); //this function returns the content of a file.
  unlink($old);
  $file = fopen($new, "w");
  fwrite($file,$content);
  fclose($file);
}

發生相同的錯誤。 所有重命名的文件將被刪除。 那讓我覺得我做的“可變沙拉”有一個愚蠢的錯誤。 但是我不知道是什么。 回顯的結果對我來說是正確的。 PHP沒有給我任何有關該問題的警告。

回顯結果:

來源:“ data / directory_exemple / 47.txt”目標:“ data / directory_exemple / 46.txt”

謝謝您的幫助!

編輯:看起來只有文件之一被重命名。 其余的將被刪除。 也許是邏輯錯誤。

您下面的功能正常工作

function myRename($old, $new){
  $content = printFile($old); //this function returns the content of a file.
  unlink($old);
  $file = fopen($new, "w");
  fwrite($file,$content);
  fclose($file);
}

我已經用示例文件測試了您的myRename()函數,它會重命名。

確保您具有正確的路徑,從printFile()獲取內容並具有寫權限

暫無
暫無

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

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