Content-Length is wrong

From Noah.org
Jump to navigationJump to search


If you are trying to use a PHP script to send files to force them to be a download instead of displayed in the browser, you may find that the Content-Length is wrong with certain browsers. You set the Content-Length, yet a sniff of the HTTP headers shows something different. The problem is almost certianly due to mod_deflate.

<Location />
    AddOutputFilterByType DEFLATE text/html text/plain text/xml
    # Netscape 4.x has some problems...
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    # Netscape 4.06-4.08 have some more problems
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    # MSIE masquerades as Netscape, but it is fine
    BrowserMatch ^HMSIE !no-gzip !gzip-only-text/html
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
</Location>

PHP download scripts usually look something like this:

...
$output = file_get_contents ($url);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($url)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".count($output));
echo $output;