使用 DOMDocument->save('filename') 保存 xml 文件时遇到问题
我有一个简短的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试创建该文件的用户不是“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.
权限被拒绝意味着您的脚本无法将数据写入 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:
Hope this helps