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.
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
However, have you ever tried to use x-sendfile?
http://wiki.nginx.org/XSendfile