简体   繁体   English

PHP:使用重命名移动失败,但是复制和取消链接的组合有效

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

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). 我正在尝试使用PHP的rename将文件移动到其他文件夹(并在同一步骤中重命名文件)。 However, rename always returns false . 但是, rename始终返回false On the other hand, using a combination of copy and unlink works just fine. 另一方面,使用copyunlink的组合就可以了。 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 eg 路径例如

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

Start by checking out whet the error was: 首先检查出错误是:

print_r(error_get_last());

What version of php are you using? 您正在使用哪个版本的php? On older versions, rename only works if both source and destination are on the same filesystem. 在旧版本中,仅当源和目标都在同一文件系统上时, rename才有效。 On some systems, rename will also fail if you have an open file descriptor for that file. 在某些系统上,如果您拥有该文件的打开文件描述符, rename也会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM