使用 DOMDocument->save('filename') 保存 xml 文件时遇到问题

发布于 2024-12-25 13:12:35 字数 1207 浏览 0 评论 0原文

我有一个简短的 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:[email protected]/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]:无法打开流:/home/yurow/wwwroot/yurowdesigns.com/programs/中的权限被拒绝xmlTest.php 第 8 行。

我已经检查了“save”命令的语法:

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

,它似乎是正确的。知道是什么原因造成的吗???

I have a short PHP program that loads an XML file (test02.xml) and then attempts to save it in another file.

The program is:

<?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:[email protected]/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");
?>

The file test03.xml opens properly and is created if it does not exist. However, there is no output in it. The loaded file, test02.xml, is non-empty and well formed XML. I have loaded and read it successfully with other PHP programs.

When I run the above program, I get the message:

Warning: DOMDocument::save(test03.xml) [domdocument.save]: failed to open stream: Permission denied in /home/yurow/wwwroot/yurowdesigns.com/programs/xmlTest.php on line 8.

I have checked the syntax of the 'save' command at:

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

and it appears to be correct. Any idea what is causing this???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2025-01-01 13:12:36

尝试创建该文件的用户不是“yurow”(可能有权创建该文件的用户)。相反,它是一个用户,例如“apache”或“httpd”。通常,系统设置为禁止 apache/httpd 用户在 Web 根目录内创建文件。这样做是出于安全目的,我不建议尝试通过授予 apache/httpd 对您的 webroot 的写访问权限来规避它。相反,您可以在 /home/yurow 内的某个位置创建文档(只是不在 /home/yurow/wwwroot 内)。例如:/home/yurow/xmldata/test02.xml。您可能需要调整此目录的权限以允许 apache/httpd 用户写入该目录。

The user trying to create the file is not "yurow" (a user who likely has permission to create the file). Rather it's a user such as "apache" or "httpd". Often, systems are set up to disallow the apache/httpd user from creating files inside the web root. This is done for security purposes and I would not recommend trying to circumvent it by giving apache/httpd write access to your webroot. Instead, you could create the document somewhere inside /home/yurow (just not inside /home/yurow/wwwroot). An example might be: /home/yurow/xmldata/test02.xml. You may need to adjust permissions on this directory to allow the apache/httpd user to write to it.

ゝ杯具 2025-01-01 13:12:36

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

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

希望这有帮助

Permission denied means your script cannot write data into the xml file because the file does not have the correct permission to write data into it. Try the following:

  1. Create empty xml file (test03.xml).
  2. Change the xml file permissions to 775 or 777 depending on your server.
  3. Save the xml data into your new file.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文