I am downloading a PDF file that is located on a server (file can be opened) via PHP script:
ini_set("zlib.output_compression", "Off"); // just to be sure
$quoted = sprintf('"%s"', addcslashes(basename($fileName), '"\\'));
$size = filesize($fileName); // size at this stage is correct
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
ob_clean();
flush();
readfile($fileName);
ini_set("zlib.output_compression", "On");
exit;
The downloaded file is gzipped. Smaller in size than original. Cannot be opened until the .gz extension is added at the end and file extracted.
Server API CGI/FastCGI
PHP version 7.1.0
Lighttpd 1.4.35
Request headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Authorization:Basic ************
Cache-Control:max-age=0
Connection:keep-alive
Host:dev.*********.lv
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Response headers
Cache-Control:must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Description:File Transfer
Content-Disposition:attachment; filename="INV_AUC15-3_U1_DDDDD-IIII_03112016.pdf"
Content-Length:831807
Content-Transfer-Encoding:binary
Content-Type:application/octet-stream
Date:Wed, 04 Jan 2017 10:22:19 GMT
Expires:0
Last-Modified:Wed, 04 Jan 2017 10:22:19 GMT
Pragma:public
Server:FS
X-Powered-By:PHP/7.1.0
The Content-Length header is correct (the same as on file system).
If I put in full URL pointing to PDF file, the file is downloaded and opened without any problems.
Why or when is the target file silently gzipped after/before it is downloaded?!
Tested on Chrome, Firefox, Safari, Opera