title: XML File Contents Disappearing with PHP Fatal Error
body: <p>I have a series of scripts that load data from an XML document and save changes to it. This occurs daily, generally around 300 times per day. I have a very strange occurrence that has happened twice now. It's not a lot, but it's very important that this file has 100% uptime and I'm really baffled as to what is happening since it works without issue 364 days per year. The XML file completely wipes except for the first line. The error log follows. The first fatal error happened on the 20th, but the XML file actually didn't wipe until the 23rd.</p>
  <pre><code>[20-Feb-2021 05:53:40 America/Vancouver] PHP Warning:  DOMDocument::load(): Document is empty in yyy.xml, line: 1 in xxx.php on line 161
  [20-Feb-2021 05:53:40 America/Vancouver] PHP Fatal error:  Uncaught Error: Call to a member function setAttribute() on null in xxx.php:400
  Stack trace:
  #0 {main}
    thrown in xxx.php on line 400
  [23-Feb-2021 02:58:11 America/Vancouver] PHP Warning:  DOMDocument::load(): attributes construct error in yyy.xml, line: 351 in xx2.php on line 155
  [23-Feb-2021 02:58:11 America/Vancouver] PHP Warning:  DOMDocument::load(): Couldn't find end of Start Tag trail line 351 in yyy.xml, line: 351 in xx2.php on line 155
  [23-Feb-2021 02:58:11 America/Vancouver] PHP Fatal error:  Uncaught Error: Call to a member function setAttribute() on null in xx2.php:383
  Stack trace:
  #0 {main}
    thrown in xx2.php on line 383
  </code></pre>
  <p>Line 161:</p>
  <pre><code>$xml-&gt;load('yyy.xml');
  </code></pre>
  <p>Surrounding code:</p>
  <pre><code>ini_set('display_errors', 'On'); ini_set('html_errors', 0); error_reporting(-1);
  $xml = new DOMDocument('1.0', 'utf-8');
  $xml-&gt;formatOutput = true; 
  $xml-&gt;preserveWhiteSpace = false;
  $xml-&gt;load('yyy.xml');
  $xpath = new DOMXpath($xml);
  </code></pre>
  <p>Line 400 and surrounding lines:</p>
  <pre><code>  foreach ($_POST[&quot;form&quot;] as $x=&gt;$state) {
      $form2-&gt;item($x)-&gt;setAttribute(&quot;status&quot;, $state);
    }
  </code></pre>
  <p>Line 155:</p>
  <pre><code>$xml-&gt;load('yyy.xml');
  </code></pre>
  <p>From what I can see here, it looks as if it didn't correctly load the document on the 20th, however, the users were loading and saving the XML file without issue until this error shows up again on the 23rd. I don't know at which point the contents of the file were wiped and I don't know what the cause would be?</p>
  <p>Edit:
  It's possible that the device loading the XML file may have had an intermittent internet connection. Would it be possible that it didn't properly load the XML file and therefore was missing a piece that it tried to save back? Would there be a way to check if a document is fully loaded?</p>
  <p>Edit2 for requested code:</p>
  <pre><code>$forms = $xpath-&gt;query(&quot;//areas/area[@name='XXX']/trails/trail[@difficulty='novice']&quot;);
  ---
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    foreach ($_POST[&quot;form&quot;] as $x=&gt;$state) {
      form2-&gt;item($x)-&gt;setAttribute(&quot;status&quot;, $state);
    }
    $xml-&gt;save('1756.xml');
    $fp = fopen('savelog.txt', 'a');
    fwrite($fp, $_SERVER['REMOTE_USER'] . &quot; @ &quot; . $timestamp . &quot; from XXX&quot; . PHP_EOL);
    fclose($fp);
  }
  ---
  &lt;?php foreach ($form2 as $form21): ?&gt;
                    &lt;h4&gt;&lt;?= htmlspecialchars($form-&gt;getAttribute(&quot;name&quot;)) ?&gt;&lt;/h4&gt;
                    &lt;select name=&quot;form[]&quot;&gt;
                      &lt;option selected value=&quot;&lt;?= htmlspecialchars($form21-&gt;getAttribute(&quot;status&quot;)) ?&gt;&quot; style=&quot;display: none;&quot;&gt;
                        &lt;?= htmlspecialchars($form21-&gt;getAttribute(&quot;status&quot;)) ?&gt;
                      &lt;/option&gt;
                      &lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
                      &lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
                      &lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
                      &lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;
                      &lt;option value=&quot;5&quot;&gt;5&lt;/option&gt;
                    &lt;/select&gt;
  &lt;?php endforeach; ?&gt;
  
  </code></pre>