Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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???

like image 448
user1107199 Avatar asked Jan 05 '12 22:01

user1107199


2 Answers

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.

like image 176
Rob Apodaca Avatar answered Sep 27 '22 23:09

Rob Apodaca


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

like image 38
satrun77 Avatar answered Sep 27 '22 23:09

satrun77