title: Trouble editing Word document with PHP
body: <ol>
  <li>I want to open a word document and edit it</li>
  <li>I am opening the word document from the server and at that time it's opening with garbage values (perhaps it's not being properly converted to UTF-8).</li>
  <li>When I delete those garbage values and insert something from a textarea to that file it is going to insert and from then on it opens properly.</li>
  <li>I would like the document to open with the English words in the document instead of garbage value - it's only the first opening that's broken at present.</li>
  </ol>
  
  
  
  <pre><code>&lt;?
  
  $filename = 'test.doc';
  
  if(isset($_REQUEST['Submit'])){
      $somecontent = stripslashes($_POST['somecontent']);
      // Let's make sure the file exists and is writable first.
      if (is_writable($filename)) {
  
          // In our example we're opening $filename in append mode.
          // The file pointer is at the bottom of the file hence
          // that's where $somecontent will go when we fwrite() it.
          if (!$handle = fopen($filename, 'w')) {
              echo "Cannot open file ($filename)";
              exit;
          }
  
          // Write $somecontent to our opened fi&lt;form action="" method="get"&gt;&lt;/form&gt;le.
          if (fwrite($handle, $somecontent) === FALSE) {
              echo "Cannot write to file ($filename)";
              exit;
          }
  
          echo "Success, wrote ($somecontent) to file ($filename) &lt;a href=".$_SERVER['PHP_SELF']."&gt; - Continue - ";
  
          fclose($handle);
  
      } else {
          echo "The file $filename is not writable";
      }
  } else {
      // get contents of a file into a string
  
      $handle = fopen($filename, 'r');
  
      $somecontent = fread($handle, filesize($filename));
  
  
      ?&gt;
  &lt;h1&gt;Edit file &lt;? echo $filename ;?&gt;&lt;/h1&gt;
  &lt;form name="form1" method="post" action=""&gt;
  &lt;p&gt;
  &lt;textarea name="somecontent" cols="80" rows="10"&gt;&lt;? echo $somecontent ;?&gt;&lt;/textarea&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;input type="submit" name="Submit" value="Submit"&gt;
  &lt;/p&gt;
  &lt;/form&gt;
  
  &lt;?
      fclose($handle);
  }
  ?&gt;
  </code></pre>