通过 PHP 下载后的文件 gzip
File gzipped after downloaded via PHP
我正在通过 PHP 脚本下载位于服务器上的 PDF 文件(文件可以打开):
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;
下载的文件已压缩。尺寸比原装小。在末尾添加 .gz 扩展名并提取文件后才能打开。
服务器APICGI/FastCGI
PHP 版本 7.1.0
Lighttpd 1.4.35
请求header
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
响应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
Content-Length header 是正确的(与文件系统相同)。
如果我完整地 URL 指向 PDF 文件,该文件将被下载并打开,没有任何问题。
目标文件为何或何时被静默压缩 after/before 它已下载?!
已在 Chrome、Firefox、Safari、Opera
上测试
出于某种原因
ini_set("zlib.output_compression", "Off");
没有完成它的工作,变量保持 "On"。尽管这个参数是 PHP_INI_ALL class 并且 entry 可以在任何地方设置。
在 php.ini 文件中将此变量设置为 Off 解决了问题,但也引发了有关运行时配置变量的新问题。
正在调用 phpinfo();在 readfile() 允许我追踪错误之前。
我正在通过 PHP 脚本下载位于服务器上的 PDF 文件(文件可以打开):
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;
下载的文件已压缩。尺寸比原装小。在末尾添加 .gz 扩展名并提取文件后才能打开。
服务器APICGI/FastCGI
PHP 版本 7.1.0
Lighttpd 1.4.35
请求header
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
响应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
Content-Length header 是正确的(与文件系统相同)。
如果我完整地 URL 指向 PDF 文件,该文件将被下载并打开,没有任何问题。
目标文件为何或何时被静默压缩 after/before 它已下载?!
已在 Chrome、Firefox、Safari、Opera
上测试出于某种原因
ini_set("zlib.output_compression", "Off");
没有完成它的工作,变量保持 "On"。尽管这个参数是 PHP_INI_ALL class 并且 entry 可以在任何地方设置。
在 php.ini 文件中将此变量设置为 Off 解决了问题,但也引发了有关运行时配置变量的新问题。
正在调用 phpinfo();在 readfile() 允许我追踪错误之前。