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

走远了吗. 提交于 2019-12-23 01:19:12

问题


I am trying to use PHP's rename to move a file to a different folder (and also rename the file in the same step). However, rename always returns false. On the other hand, using a combination of copy and unlink works just fine. What could be causing this?

The relevant code looks like this:

  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");
  }

The paths would be e.g.

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

回答1:


Start by checking out whet the error was:

print_r(error_get_last());

What version of php are you using? On older versions, rename only works if both source and destination are on the same filesystem. On some systems, rename will also fail if you have an open file descriptor for that file.



来源:https://stackoverflow.com/questions/25076006/php-move-with-rename-fails-but-combination-of-copy-and-unlink-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!