title: Downloading .docx file
body: <p>I am using phpdocx to create a word document.  It is a valid word document when I look at it on the server.  However, when I try to download it, it says that the file cannot be opened because there are problems with the file.  Word is able to recover the document, but it clients will complain about an invalid word document.  Here is the code I use to download the file:</p>
  
  <pre><code>function readfile_chunked_remote($filename, $seek = 0, $retbytes = true, $timeout = 3) {
      set_time_limit(0); 
      $defaultchunksize = 1024*1024; 
      $chunksize = $defaultchunksize; 
      $buffer = ''; 
      $cnt = 0; 
      $remotereadfile = false; 
  
  
  if (preg_match('/[a-zA-Z]+:\/\//', $filename)) 
  $remotereadfile = true; 
  
  $handle = @fopen($filename, 'rb'); 
  
  if ($handle === false) { 
  return false; 
  } 
  
  stream_set_timeout($handle, $timeout); 
  
  if ($seek != 0 &amp;&amp; !$remotereadfile) 
  fseek($handle, $seek); 
  
  while (!feof($handle)) { 
  
  if ($remotereadfile &amp;&amp; $seek != 0 &amp;&amp; $cnt+$chunksize &gt; $seek) 
      $chunksize = $seek-$cnt; 
  else 
      $chunksize = $defaultchunksize; 
  
  $buffer = @fread($handle, $chunksize); 
  
  if ($retbytes || ($remotereadfile &amp;&amp; $seek != 0)) { 
      $cnt += strlen($buffer); 
  } 
  
  if (!$remotereadfile || ($remotereadfile &amp;&amp; $cnt &gt; $seek)) 
      echo $buffer; 
  
  ob_flush(); 
  flush(); 
  } 
  
  $info = stream_get_meta_data($handle); 
  
  $status = fclose($handle); 
  
  if ($info['timed_out']) 
  return false; 
  
  if ($retbytes &amp;&amp; $status) { 
  return $cnt; 
  } 
  
  return $status; 
  </code></pre>
  
  <p>}</p>
  
  <p>Thanks!</p>