PHP Force downloding显示网络错误

PHP Force downloding显示网络错误,php,force-download,Php,Force Download,我尝试使用PHP强制下载进行文档下载,并使用了以下代码 header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: Binary"); header("Content-Length: ".filesize($path) ."\""); header("Content-Disposition: attachment; filename=\"".basename($path)."

我尝试使用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
这:

此标题中的输出:

Content-Length:"
替换为:

header("Content-Length: ".filesize($path));
并修复
$path
,该路径也不正确,因为它返回
null
(或
false

输出应该类似于:

Content-Length: 10032219
此外,PDF的正确mime类型为:

 Content-Type: application/pdf
你应该删除

ob_clean();
flush();

在读取和发送文件之前,没有理由刷新输出。esp flush()命令可能导致错误/删除
谢谢..之前的任何空格或新行。。它下载了文件,但无法打开。你能再帮我一次吗?@Danfrom德国非常感谢。这条路不对。我再次解决了这个问题,因为有很多文档类型,比如jpg、png、docx等,这就是我使用内容类型的原因:application/octetstream。现在我不能只下载pdf文件,但不能下载其他格式,即使我使用的是内容类型应用程序/octet-stream@MufeedKKasim看看哪一个检测到mime类型的文件,这样你就可以自动地一直纠正一个<代码>标题(“内容长度:”.filesize($path))-这拯救了我的一天,因为我移除了ob_肉体();清洁();但它仍然没有打开图像和.docx文件,而是以全尺寸下载。我在notepad++中打开,没有PHP错误。但无法打开文件。奇怪的是,我可以下载.pdf文件,这对我很有效
ob_clean();
flush();