PHP:使用重命名移动失败,但是复制和取消链接的组合有效 [英] PHP: move with rename fails, but combination of copy and unlink works

查看:105
本文介绍了PHP:使用重命名移动失败,但是复制和取消链接的组合有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP的rename将文件移动到另一个文件夹(并在同一步骤中重命名该文件).但是,rename始终返回false.另一方面,结合使用copyunlink可以正常工作.可能是什么原因造成的?

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?

相关代码如下:

  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也会失败.

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.

这篇关于PHP:使用重命名移动失败,但是复制和取消链接的组合有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆