我尝试使用PHP强制下载进行文档下载,并使用下面的代码
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($path) ."\"");
header("Content-Disposition: attachment; filename=\"".basename($path)."\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
ob_clean();
flush();
readfile($path);
exit;
但是当我运行脚本时它只显示下载开始...并在chrome中显示“网络错误”,当我检查网络控制台时它显示的内容
Request URL:http://websddress.com/presse/index.php?option=com_presse&task=viewDoc&id=2578&lg=FR
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Cookie:__utma=139058359.696419680.1380560986.1380711878.1380731571.5; __utmz=139058359.1380560986.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); 58a32855c678fbf6131983dab57747c9=uv0bipblgn8bd3diqbd52dn9g1; 64cf32705845a188b511407ba5bcd3d6=edbgk8jtalamqnininr89qb327
Host:5.135.115.74
Referer:http:webaddress.com/presse/index.php?option=com_presse&task=viewDocs&secid=787&lg=fr
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Query String Parametersview sourceview URL encoded
option:com_presse
task:viewDoc
id:2578
lg:FR
Response Headersview source
Cache-Control:no-cache, must-revalidate
Content-Disposition:attachment; filename="Advanced_Fair_-_Grohe_september_doc_FR_2578.pdf"
Content-Length:"
Content-Transfer-Encoding:Binary
Content-Type:application/octet-stream
Date:Thu, 21 Nov 2013 15:49:29 GMT
Expires:0
P3P:CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
X-Powered-By:PHP/5.3.23
答案 0 :(得分:1)
此:
header("Content-Length: ".filesize($path) ."\"");
标题输出:
Content-Length:"
替换为:
header("Content-Length: ".filesize($path));
并修正$path
这也不正确,因为它会返回null
(或false
)。
输出应该是这样的:
Content-Length: 10032219
此外,PDF的正确mime类型是:
Content-Type: application/pdf
答案 1 :(得分:1)
你应该删除
ob_clean();
flush();
在阅读和阅读之前没有理由冲洗输出。发送文件。 esp flush()命令可能导致错误/删除<?php
标记之前的任何空格或新行