簡體   English   中英

PHP:使用重命名移動失敗,但是復制和取消鏈接的組合有效

[英]PHP: move with rename fails, but combination of copy and unlink works

我正在嘗試使用PHP的rename將文件移動到其他文件夾(並在同一步驟中重命名文件)。 但是, rename始終返回false 另一方面,使用copyunlink的組合就可以了。 是什么原因造成的?

相關代碼如下:

  if (!rename($targetpath, $backuppath)) {
    // if rename fails, try with copy and delete
    if (!copy($targetpath, $backuppath)) 
      die("9\nCould not move existing file to backup");
    touch($backuppath, filemtime($targetpath));
    if (!unlink($targetpath))
      die("9\nCould not move existing file to backup");
  }

路徑例如

$targetpath: /path/to/plots/some.pdf
$backuppath: /path/to/plots/old/some.pdfX14068815860

首先檢查出錯誤是:

print_r(error_get_last());

您正在使用哪個版本的php? 在舊版本中,僅當源和目標都在同一文件系統上時, rename才有效。 在某些系統上,如果您擁有該文件的打開文件描述符, rename也會失敗。

暫無
暫無

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

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