I have the following script to let a visitor download a file:
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();
It doesn't work very well. (I've also put the filename in quotes, same result).
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%.
The server is a shared host, Mac OS X server.
With using Firefox's Live HTTP headers add-on, I've noticed the server adds aditional headers to the response:
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 // <-- expecially this one,
Vary: Accept-Encoding // <-- this one,
MS-Author-Via: DAV // <-- and this one
Keep-Alive: timeout=10, max=100
Connection: Keep-Alive
Content-Type: application/octet-stream
Could these be the cause of the problem?
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.
I'm really rather clueless on this one. Your help is appeciated. Thank you in advance.
UPDATE:
I think I have narrowed the problem down to the bottleneck. The webserver automatically gzip compresses the output. When I removed the Content-Length
header from my PHP script everything started downloading smooth. This makes sense: The value of the Content-Length
doesn't match the actual gzipped output anymore. In PHP I read the uncompressed filesize to set the Content-Length
header, but afterwards, Apache compresses it, and this is probably where the browsers choked.
I'll follow this question up with a question about how to set the correct Content-Length
header size when the webserver automatically gzip compresses output.