2010-09-01 53 views
1

我让我的用户下载使用该代码的PDF文件:下载PDF - 不能打开它

$db->Record['file']包含文件的相对路径。

访问一个加载pdf的虚拟页面,所以他们实际上并没有在url中看到“text.pdf”。我改变了文件的标题来实现这一目标:

$downloadfile = '/data/srv/www/vhosts/htdocs'.$db->Record['file']; 
$filename = basename($downloadfile);   
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename="'.$filename.'"');   
readfile($downloadfile); 

下载的工作没有任何问题,但是当我尝试打开它,下面的错误ocurrs:

The PDF file could not be openend, the type is not supported or the file 
was damaged. 

我怎样才能解决这个问题?

不是:当我直接下载文件时,意思是通过http://.../text.pdf访问并打开它,一切工作正常。

+2

下载的文件实际上包含什么? – 2010-09-01 11:15:11

+2

我会打赌啤酒里有PHP错误信息在文件中。 – 2010-09-01 11:17:32

回答

2

(从注释复制)我敢打赌,啤酒也有在文件中PHP错误信息。

3

浏览器可能无法从文件中知道预期读取的长度。从php documentation,尝试在报头附加文件大小:

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

您是否在第一行输入htdocs后错过了/