0

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

$file='file.pdf';
$filepath="/path-to-download-folder/$file";

if(!file_exists($filepath)){
  header('HTTP/1.1 404 Not Found');
  exit;
}elseif(!is_file($filepath) or !is_readable($filepath)){
  header('HTTP/1.1 403 Forbidden');
  exit;
}else{
    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
    readfile($filepath);
    exit;
}

However some people are receiving a corrupted PDF file.

For example: http://mlkshk.com/r/8FGS

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...

Thanks.

2
  • 1
    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, 2011 at 18:46
  • 1
    RTFM: php.net/manual/en/function.set-time-limit.php 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 [..] Oct 17, 2011 at 18:55

1 Answer 1

2

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

http://wiki.nginx.org/NginxHttpGzipModule#gzip_types

gzip_types
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?

http://wiki.nginx.org/XSendfile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .