title: How to save a new XML DomDocument to file with PHP
body: <p>I have an HTML form that gets sent to a PHP file for processing.</p>
  <p>The PHP file displays the form data in a table, and is suppose to construct an XML document and save it to a file on disk. The PHP file displays the table, but does not save anything to disk.</p>
  <p>The computer is a common PC with I3 processor, 8gb RAM, Ubuntu 20.04, web server is LAMP installed with standard apt-get. I checked every line during install and no errors.</p>
  <p>My web server and PHP all seem to be working fine.</p>
  <p>This is not a production or commercial system. Its just a hobby project.</p>
  <p>I have been reading examples for about a week now and none of them lead to a saved file of any sort.</p>
  <p>Here is the current code in the two files. (these are not the actual files I am trying to work with, as those are much longer. The files posted here contain the same structure only much shorter. The same problem exists no matter what files I am using.)</p>
  <p>test.html</p>
  <pre><code> &lt;!DOCTYPE html&gt;
   &lt;html&gt;
       &lt;head&gt;&lt;/head&gt;
       &lt;body&gt;
  
       &lt;h1&gt;A Simple Form&lt;/h1&gt;
  
       &lt;form action=&quot;test.php&quot; method=&quot;post&quot;&gt;
  
           Name:           &lt;input type=&quot;text&quot; name=&quot;persons_name&quot;&gt;&lt;br&gt;
           Address:        &lt;input type=&quot;text&quot; name=&quot;persons_address&quot;&gt;&lt;br&gt;
           Phone Number:   &lt;input type=&quot;text&quot; name=&quot;persons_phone_number&quot;&gt;&lt;br&gt;
      
      
           &lt;input type=&quot;submit&quot;&gt;  
  
       &lt;/form&gt;
  
  
       &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  <p>test.php</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
  
          &lt;h1&gt;How to save HTML form data to XML file with PHP&lt;/h1&gt;
  
  
          &lt;table&gt;
              &lt;tr&gt;&lt;td&gt;Name:           &lt;?php   echo $_POST[&quot;persons_name&quot;];?&gt;  &lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;Address:        &lt;?php   echo $_POST[&quot;persons_address&quot;];?&gt;  &lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;Phone Number:   &lt;?php   echo $_POST[&quot;persons_phone_number&quot;];?&gt;  &lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;
  
  
          &lt;?php
          
         
              //create a dom document with encoding utf8
              $domDocument = new DOMDocument();
                  $domDocument-&gt;encoding = &quot;utf-8&quot;;
                  $domDocument-&gt;xmlVersion = &quot;1.0&quot;;
                  $domDocument-&gt;formatOutput = true;
  
              //use a variable to store the filename. This could be useful if the filename is related to something in the form data, such as the persons name.
              $xml_file_name = &quot;test.xml&quot;;
  
  
              //create the root element of the xml tree
              $xmlRoot = $domDocument-&gt;createElement(&quot;xml&quot;);
          
              //append it to the document created
              $xmlRoot = $domDocument-&gt;appendChild($xmlRoot);
  
              //Create All of the Tags
              $personsName = $domDocument-&gt;createElement(&quot;persons_name&quot;);
              $domDocument-&gt;appendChild($personsName);
          
              $personsAddress = $domDocument-&gt;createElement(&quot;persons_address&quot;);
              $domDocument-&gt;appendChild($personsAddress);
          
              $personsPhoneNumber = $domDocument-&gt;createElement(&quot;persons_phone_number&quot;);
              $domDocument-&gt;appendChild($personsPhoneNumber);
      
      
              // save as file
              $domDocument-&gt;save($xml_file_name);
                  
          
          ?&gt;
      
      
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>UPDATE EDIT: 30 June 2020</p>
  <p><strong>File permissions:</strong> Before I posted this question I changed the permissions on all files and directories to everyone can do anything from www/html down.</p>
  <p><strong>sudo chmod 777 -R www</strong></p>
  <p>Since this is merely a hobbyist test server wide open permissions are not a security problem.</p>
  <p>As per the suggestions for those who posted (thanks guys) I enabled PHP logging. I should have know better in the first place. It turns out that Apache is doing all the logging and has been enabled since I started this project. So...</p>
  <p>I misunderstood that the DOM environment and XML functions were installed as part of the PHP core. What that meant to me was installed when PHP was installed. Not true, at least on my system.</p>
  <p>The error log is stored in /var/log/apache2/error.log
  I found many of the following line in that log (this is just a fragment. I left out ip address, my website path, etc):</p>
  <p><strong>PHP Fatal error:  Uncaught Error: Class 'DOMDocument' not found in /var/www/html/</strong></p>
  <p>Researching this error I discovered I had to install the DOM and XML functions for PHP</p>
  <p>So...</p>
  <p>For Ubuntu 20.04 (my system) do the following:</p>
  <p><strong>sudo apt-get install php-dom</strong></p>
  <p><strong>sudo apt-get install php-xml</strong></p>
  <p>Immediately after I rebooted the computer and submitted the test form above the XML file was created in the directory where the php file lives.</p>
  <p>However, it is really malformed so I need to do some work on the code itself. Will update this posting again when I get the code all straightened out.</p>