1

Following my previous question, in which I think I've narrowed my problem down to the bottleneck:

How do I set the correct value for the Content-Length header for a downloadable file, from within PHP, when the webserver (apache) automatically compresses the ouput afterwards?

I'm looking for the most robust/generic solution. I'm not well informed about output compression, but I presume apache has several compression algorithms it can utilize (gzip, etc?). So, from within PHP, how would I automatically discover what compression the webserver uses, and how can I use this to set the correct Content-Length header value for the filesize of the file (after it's been compressed)?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106

3 Answers3

5

You don’t need to specify Content-Length, Apache does that for you.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • That's what I assumed also, since Apache does the compressing, but I don't see it show up in FireFox's Live HTTP headers add-on. Are you sure this is the case? – Decent Dabbler Feb 18 '10 at 11:30
  • @fireeyedboy: Hm, this must be an issue with your Apache. Because my local Apache sets the *Content-Length* field when using mod_deflate. – Gumbo Feb 18 '10 at 14:02
  • 4
    Apache won't set Content-Length if it's using chunked encoding, which it will use for PHP output if mod_deflate is off, *or* if the size of the content is greater than the DeflateBufferSize setting, or if mod_deflate isn't being used for PHP in your configuration. – orrd Sep 08 '15 at 02:41
0

Maybe cletus's answer in this question helps.

Also, are you sure you want to serve your downloads using gzip? Zipping makes much sense for HTML, CSS and JS contents, but with huge file downloads, I would turn it off.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • *Content-Length* refers to the length of the HTTP message body. And if the message body is compressed, the *Content-Length* value reflects that. – Gumbo Feb 18 '10 at 11:00
  • Cheers @Gumbo, I wasn't sure myself. – Pekka Feb 18 '10 at 11:01
0

Okay, so the situation here is you issue a file download. I bet that file is already compressed to save you some bandwidth.

Anyway, if your server has any compression active, that's not good, as it spends time for nothing as the server can't achieve a better compression, so for speeding this up, you should disable output compress for any file download.

try from these:

apache conf
Disable mod_deflate

php.ini
output_buffering = Off
output_handler =
zlib.output_compression = Off

Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • I wouldn't recommend turning off zlib and mod_deflate for an entire server or website just so it doesn't also do some potentially unnecessary compressing of downloadable files. – orrd Sep 08 '15 at 19:45