Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this code to serve a file for download via PHP:


  header('HTTP/1.1 404 Not Found');
}elseif(!is_file($filepath) or !is_readable($filepath)){
  header('HTTP/1.1 403 Forbidden');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Content-Type: '.mime_content_type($filepath));
    header('Content-Length: '.filesize($filepath));
    header('Content-Disposition: attachment; filename="'.$file.'"');

    set_time_limit(0); // Big files/slow connections may result in incomplete downloads

However some people are receiving a corrupted PDF file.

For example:

In Nginx I have this:

    gzip on;
    gzip_disable "msie6";
    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

My question is: how to set it not to gzip PDF files? Maybe this is the problem...


share|improve this question
Do you really need the set_time_limit(0)? Surely it would be better to allow the script to run, setting it to 0 seconds seems like it should stop execution instantly, explaining why your files are corrupt since the client isn't actually receiving any data, just headers. IMO you should try setting it to 30 seconds or something, giving the server time to actually read and send the file before being halted. – gac Oct 17 '11 at 18:46
RTFM: and however is useless: [..] Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time [..] – Giovanni Toraldo Oct 17 '11 at 18:55
up vote 2 down vote accepted

With your current configuration you are not gzipping PDFs, but you can check it using some tool like Firebug or Google PageSpeed

syntax: gzip_types mime-type [mime-type ...]

default: gzip_types text/html

context: http, server, location

Enables compression for additional MIME-types besides "text/html". "text/html" is always compressed.

However, have you ever tried to use x-sendfile?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.