我有一个简短的 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");
?>
Run Code Online (Sandbox Code Playgroud)
文件 test03.xml 正确打开并在它不存在时创建。但是,其中没有输出。加载的文件 test02.xml 是非空且格式良好的 XML。我已经用其他 PHP 程序成功加载并阅读了它。
当我运行上述程序时,我收到消息:
警告:DOMDocument::save(test03.xml) [domdocument.save]:无法打开流:第 8 行 /home/yurow/wwwroot/yurowdesigns.com/programs/xmlTest.php 中的权限被拒绝。
我在以下位置检查了“保存”命令的语法:
phpbuilder.com/manual/en/function.dom-domdocument-save.php
它似乎是正确的。知道是什么原因造成的吗???
尝试创建该文件的用户不是“yurow”(可能有权创建该文件的用户)。相反,它是一个用户,例如“apache”或“httpd”。通常,系统设置为禁止 apache/httpd 用户在 Web 根目录内创建文件。这样做是出于安全目的,我不建议尝试通过授予 apache/httpd 对您的 webroot 的写访问权限来规避它。相反,您可以在 /home/yurow 内的某个位置创建文档(只是不在 /home/yurow/wwwroot 内)。例如:/home/yurow/xmldata/test02.xml。您可能需要调整此目录的权限以允许 apache/httpd 用户写入该目录。