2014-09-26 69 views
0
$file = "http://www.abax.se/content/download/137/862/file/example.pdf"; 
header('Content-Description: File Transfer'); 
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
flush(); 
readfile($file); 

这是我正在运行的代码,我出来的想法,任何解决方案?PHP下载给我空PDF

+1

删除'平齐()'? – Daan 2014-09-26 13:30:39

+0

同样的东西,空的下载pdf ... – josaric 2014-09-26 13:38:01

+0

这段代码看起来很奇怪。为什么要下载文件才能上传?你为什么不直接发送“301永久移动”响应,将用户直接发送到文件?该文件是否与此代码位于同一台服务器上?如果是这样,那么你不应该通过HTTP;您应该直接从服务器读取并输出文件。 – 2014-09-26 13:38:08

回答

0

删除header('Content-Length: ' . filesize($file));

那是什么打破你的输出。

filesize()需要一个本地文件,而不是一个url。

工作代码:

<?php 
$file = "http://www.abax.se/content/download/137/862/file/example.pdf"; 
header('Content-Description: File Transfer'); 
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
flush(); 
readfile($file); 
?> 
+0

“无法加载PDF文档” – josaric 2014-09-26 13:39:14

+0

它虽然在这里工作.. – 2014-09-26 13:40:25

+0

添加了工作代码的答案。这在php5.4上工作正常。 – 2014-09-26 13:45:46