【问题标题】:File gzipped after downloaded via PHP通过 PHP 下载后压缩的文件
【发布时间】:2017-01-04 10:39:58
【问题描述】:

我正在通过 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;

下载的文件被 gzip 压缩。尺寸比原版小。在末尾添加 .gz 扩展名并提取文件之前无法打开。

服务器 API CGI/FastCGI

PHP 版本 7.1.0

Lighttpd 1.4.35

请求标头

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

响应标头

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 标头正确(与文件系统上的相同)。

如果我输入指向 PDF 文件的完整 URL,文件将被下载并打开,没有任何问题。

为什么或何时在下载目标文件之后/之前对其进行静默 gzip 压缩?!

在 Chrome、Firefox、Safari、Opera 上测试

【问题讨论】:

    标签: php download gzip zlib


    【解决方案1】:

    由于某种原因

    ini_set("zlib.output_compression", "Off");
    

    没有完成它的工作,变量保持“打开”。即使此参数属于PHP_INI_ALL 类,也可以在任何地方设置条目。

    在 php.ini 文件中将此变量设置为 Off 解决了这个问题,但也提出了一个关于运行时配置变量的新问题。

    调用 phpinfo();在 readfile() 允许我追踪错误之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      相关资源
      最近更新 更多