2013-11-21 44 views
1

我尝试使用PHP力下载,使文件下载和我用下面的代码PHP力downloding显示网络错误

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; 

但是当我运行该脚本只是显示开始下载...并显示“网络错误”,在镀铬,当我检查网络控制台它显示了这样的事情

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 

回答

1

此:

header("Content-Length: ".filesize($path) ."\""); 

输出在标题这样的:

Content-Length:" 

通过替换:

header("Content-Length: ".filesize($path)); 

并固定$path这也是不正确,因为它返回null(或false)。

输出应该是这样的:

Content-Length: 10032219 

而且,正确的MIME类型的PDF是:

Content-Type: application/pdf 
+0

对不起,我很好地检查代码。无论如何其工作现在 –

+0

谢谢..它下载的文件,但它不能打开。你能再次帮助我吗?? –

+0

@MufeedKKasim查看关于content-type的更新 – DanFromGermany

1

你应该删除

ob_clean(); 
flush(); 

没有理由在读取&发送文件之前清空输出。 esp的flush()命令可能会导致错误/删除任何空格或<?php标签之前的新行

+0

Yess我删除了ob_flesh();清洁();但它仍然没有打开图像和.docx文件。它以全尺寸下载。我用记事本++打开,没有PHP错误。但无法打开文件。奇怪的是可以下载.pdf文件 –