title: Problem with downloads: slow and/or fails
body: <p>I have the following script to let a visitor download a file:</p>
  
  <pre><code>header( 'Content-Type: application/octet-stream' );
  header( 'Content-Transfer-Encoding: binary' );
  header( 'Content-Disposition: attachment; filename=' . $fileName );
  header( 'Content-Length: ' . filesize( $filePath ) );
  header( 'Content-Description: Download' );
  header( 'Cache-Control: private' );
  header( 'Pragma: no-cache' );
  header( 'Expires: 0' );
  
  readfile( $filePath );
  exit();
  </code></pre>
  
  <p>It doesn't work very well. (I've also put the filename in quotes, same result).</p>
  
  <p>It behaves very slow, and sometimes the downloads even halt. In Opera especially, it halts on 99% of the download. Sometimes it even immediately shows 99% completed, then it starts downloading and halts at around 34%.</p>
  
  <p>The server is a shared host, Mac OS X server.</p>
  
  <p>With using Firefox's Live HTTP headers add-on, I've noticed the server adds aditional headers to the response:</p>
  
  <pre><code>HTTP/1.1 200 OK
  Date: Thu, 18 Feb 2010 09:27:25 GMT
  Server: Apache
  X-Powered-By: PHP/5.2.12
  Content-Transfer-Encoding: binary
  Content-Disposition: attachment; filename=test.psd
  Content-Length: 398635
  Content-Description: Download
  Cache-Control: private
  Pragma: no-cache
  Expires: 0
  Content-Encoding: gzip // &lt;-- expecially this one,
  Vary: Accept-Encoding // &lt;-- this one,
  MS-Author-Via: DAV // &lt;-- and this one
  Keep-Alive: timeout=10, max=100
  Connection: Keep-Alive
  Content-Type: application/octet-stream
  </code></pre>
  
  <p>Could these be the cause of the problem?</p>
  
  <p>When I run the script on my localhost everything works fine. Also, when I directly download files from this host, the speed is also fine and fluent.</p>
  
  <p>I'm really rather clueless on this one. Your help is appeciated. Thank you in advance.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I think I have narrowed the problem down to the bottleneck. The webserver automatically gzip compresses the output. When I removed the <code>Content-Length</code> header from my PHP script everything started downloading smooth. This makes sense: The value of the <code>Content-Length</code> doesn't match the actual gzipped output anymore. In PHP I read the uncompressed filesize to set the <code>Content-Length</code> header, but afterwards, Apache compresses it, and this is probably where the browsers choked.</p>
  
  <p>I'll follow this question up with a question about how to set the correct <code>Content-Length</code> header size when the webserver automatically gzip compresses output.</p>