php pdf download - 无法正确打开下载的文件

时间:2013-12-07 17:10:20

标签: php pdf download

在我的网页上下载pdf文件(285kB)是可能的。问题是您将网站下载为pdf而不是我想要下载的pdf文件。

所以我不能用Adobe Reader打开下载的pdf,但必须用记事本打开文件,内容是

<!DOCTYPE html><html lang="en"><he...

以下是下载文件的代码:

 $filepath = "/download/";
    $filename = "filename";
    header('Content-disposition: attachment; filename='.$filename);
    header('Content-type: application/octet-stream' );
    header('Content-Length: '. filesize($filepath.$filename));
    readfile($filepath.$filename);
    exit;

你能帮助我吗?

3 个答案:

答案 0 :(得分:1)

您使用了错误的Content-Type标头。使用

header('Content-type: application/pdf' );

它在RFC 3778

中定义

答案 1 :(得分:1)

抱歉,由于声誉不佳,我无法发表评论。这就是发布答案的原因。你可以试试

header('Content-type: application/pdf' );
header('Content-disposition: attachment; filename='.$filename.'.pdf');
header('Content-disposition: attachment; filename='.$filename.'.pdf');

答案 2 :(得分:1)

你是否添加了带文件名的扩展名?像这样...

$filename = "filename.pdf";
$filepath = "/download/".$filename;

if (file_exists($filePath) {
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Content-type: application/zip' );
header('Content-Length: '. filesize($filePath));
readfile($filepath);
exit;
}
相关问题