使用DOMDocument保存xml文件时出现问题->;save(';filename';)


Trouble saving an xml file using DOMDocument->save('filename')

我有一个简短的PHP程序,它加载一个XML文件(test02.XML),然后尝试将其保存在另一个文件中。

程序是:

<?php
//The next three lines are needed to open a file for writing on the server I use.
$stream_options = array('ftp' => array('overwrite' => TRUE));
$stream_context = stream_context_create($stream_options);
$fxml = fopen("ftp://userid:password@www.yurowdesigns.com/public_html/programs/test03.xml","w",0,$stream_context);
//The next three lines create a new DOMDocument, load the input file, "test02.xml"  
//and attempt to save the loaded file in the output file "test03.xml"
$doc = new DOMDocument;
$doc->load("test02.xml");
$doc->save("test03.xml");
?>

文件test03.xml会正确打开,如果不存在,则会创建该文件。但是,其中没有输出。加载的文件test02.xml是非空的,格式良好的xml。我已经用其他PHP程序成功地加载并读取了它。

当我运行上面的程序时,我得到消息:

警告:DOMDocument::save(test03.xml)[DOMDocument.save]:无法打开流:第8行/home/yurow/wwwwroot/yurowdesigns.com/programs/xmlTest.php中的权限被拒绝。

我已经在以下位置检查了"保存"命令的语法:

phpbuilder.com/manual/en/function.dom-domdocument-save.php

并且它看起来是正确的。知道是什么原因造成的吗???

试图创建文件的用户不是"yurow"(可能具有创建文件权限的用户)。相反,它是诸如"apache"或"httpd"之类的用户。通常,系统设置为禁止apache/httpd用户在web根目录中创建文件。这样做是出于安全目的,我不建议通过向您的webroot提供apache/httpd写访问权限来绕过它。相反,您可以在/home/yurow中的某个位置创建文档(只是不在/home/urow/wwwwroot中)。例如:/home/yurow/xmldata/test02.xml。您可能需要调整此目录的权限,以允许apache/httpd用户对其进行写入。

拒绝权限意味着您的脚本无法将数据写入xml文件,因为该文件没有将数据写入其中的正确权限。请尝试以下操作:

  1. 创建空的xml文件(test03.xml)
  2. 根据您的服务器,将xml文件权限更改为775或777
  3. 将xml数据保存到新文件中

希望这能帮助