title: how to generate doc file using php
body: <p>I have a PHP file that has a variable named <code>$data</code>. Now I echo this variable on the page. It works fine but I want to write this variable in a doc file. How do you do this?</p>
  
  <p>I am using the following method to generate the doc but it's not working.</p>
  
  <pre><code>$fh = fopen('viewAppraisalDetailDoc.doc',"w+");
  fwrite($fh, $data); 
  $file = 'viewAppraisalDetailDoc.doc';
  
  if (file_exists($file)) {
      header('Content-Description: File Transfer');
      header('Content-Type: application/msword');
      header('Content-Disposition: attachment; filename='.basename($file));
      header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file));
      ob_clean();
      flush();
      readfile($file);
      exit;
  }
  </code></pre>