2015-09-07 153 views
0

我想下载使用PHP的PDF文件。我可以下载文本文件,图像和 我PHP代码:php pdf文件下载:无法加载PDF文档

header("Content-Type: application/octet-stream"); 

$file = $_GET["file"] .".pdf"; 
header("Content-Disposition: attachment; filename=" . urlencode($file));  
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Type: application/pdf"); // pdf might change to another format eg. doc,docx 
header("Content-Description: File Transfer");    
header("Content-Length: " . filesize($file)); 
flush(); // this doesn't really matter. 
$fp = fopen($file, "r"); 
while (!feof($fp)) 
{ 
echo fread($fp, 65536); 
flush(); // this is essential for large downloads 
} 
fclose($fp); 

这里是HTML

部分:

<a href="download.php?file=abc">Download CV</a> 

在我来说,我能下载PDF文件,它也显示内存大小,但当我打开它显示错误,如:

无法加载PDF文档

我没有得到任何错误,我试图改变内存限制和所有,但不工作。任何帮助将非常感激。

编辑:

我需要下载所有的文件格式,在我的代码中的一些简单的修改。 例如:

header("Content-Type: application/pdf"); // pdf might change to another format eg. doc,docx 

我并不想保存这是因为在其他格式的我不能直接从浏览器保存的情况下显示在浏览器的PDF文件。

当我试图从下载打开文件夹时显示错误 像:

的Adobe阅读器无法打开的文件名abc.pdf,因为它要么不支持的文件类型或因为文件已损坏

+0

如果我可能会问,为什么你需要这个头信头(“Content-Type:application/download”); – Satya

+0

[Chrome在内嵌PDF文件中有“加载PDF文档失败”错误信息](http://stackoverflow.com/questions/5670785/chrome-has-failed-to-load-pdf-document-error-message -on-inline-pdfs) – Saty

+0

@Saty不,它不能完全解决我的问题。它显示浏览器上的内容,即使我可以保存,因为它是PDF文件。但与其他格式 –

回答

0

你可以试试吗?

更改

echo fread($fp, 65536); 

echo fread($fp, filesize($file)); 

我的猜测是PDF文件的长度为你指定什么不同。

+0

感谢您的答案,但它不起作用 –