doujiang1993 2017-01-04 10:39
浏览 78
已采纳

通过PHP下载文件gzip

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

  • 写回答

1条回答 默认 最新

  • douque8855 2017-01-06 08:56
    关注

    For some reason

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

    did not do its job and the variable remained "On". Even though this parameter is of PHP_INI_ALL class and entry can be set anywhere.

    Setting this variable to Off in php.ini file solved the problem but also rised a new question regarding runtime configuration variables.

    calling phpinfo(); before readfile() allowed me to track down the bug.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP