簡體   English   中英

php pdf下載-無法正確打開下載的文件

[英]php pdf download - can't open the downloaded file correctly

在我的網頁上可以下載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;

你能幫助我嗎?

您使用了錯誤的Content-Type標頭。 采用

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

RFC 3778中定義

抱歉,由於我的聲譽低下,我無法發表評論。 這就是為什么張貼作為答案。 您可以嘗試

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

您是否在擴展名中添加了文件名? 像這樣...

$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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM