【问题标题】:unlink returns error on document that's open by DOMDocumentunlink 在 DOMDocument 打开的文档上返回错误
【发布时间】:2020-07-20 14:01:23
【问题描述】:

我试图在通过 DOMDocument 完成处理后取消链接 XML 文档,但返回如下错误:

错误

unlink(packageSettings.xml): Resource temporarily unavailable

我已经尝试了下面的代码 unsetting 那个变量,如下所示,但没有工作。

代码

$xslDoc = new \DOMDocument();
$xslDoc->load('/full/path/of/packageSettings.xml');
$xpath = new DOMXPath($xslDoc);
// do some operation
unset($xslDoc);  // I've tried this.
unlink('/full/path/of/packageSettings.xml'); // this returns above error

更新:更新了unlink 行的错字。

【问题讨论】:

  • unsetunlink?
  • “资源暂时不可用”听起来像是一条 HTTP 错误消息,而不是您从文件系统中得到的消息?
  • $xslDoc 不是文件。文件是'/full/path/of/packageSettings.xml'
  • unlink 是为了删除文件而不是取消设置变量,你想达到什么目的?
  • unlink('/full/path/of/packageSettings.xml');这将删除给定路径的文件。未设置($数组[0]);删除 $array 中索引零处存储的任何内容

标签: php xml domdocument


【解决方案1】:

我找到了其他 SO 答案,指出使用 gc_collect_cycles() 将解决问题。它确实解决了问题。所以新代码就像:

gc_collect_cycles();
unlink('/full/path/of/packageSettings.xml');

但我仍然不确定最初的根本原因是什么。

【讨论】:

  • 我最好的猜测:您的 XPath 句柄会阻止正确清理 DOMDocument 句柄,因此 unset($xslDoc); 还不够,您还需要 unset($xpath);unset($xslDoc); - 您还需要删除对在 // do some operation 期间创建的任何 DOMNode 元素,您都可以通过执行 call_user_func(function(){// do some operation }); 获得一个全新的变量范围
  • @hanshenrik 不走运。我都试过unset($xpath);unset($xslDoc);;
  • 奇怪的是我有其他 2 个 xmls 在相同的方法中,但只在这个中出错。
猜你喜欢
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多