2013-02-07 129 views
1

我正在使用以下代码开始下载,但未下载弹出窗口。相反,它显示该PDF文件的二进制格式。它在本地服务器上完美工作,但不能运行托管服务器。pdf文件未下载

 $file = '../Notes/chapter01.pdf'; 


if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    // header('Content-Type: application/octet-stream'); 
    header('Cache-Control: public'); // needed for i.e. 
    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)); 
         ob_end_clean(); 
    flush(); 
    readfile($file); 
    exit; 
} 
else { 
      die('Error: File not found.'); 
     } 
+0

看到我编辑的答案。 –

回答

0

此代码有效。

$file = '../Notes/chapter01.pdf'; 

$mime_type = 'application/pdf'; 
$filesize = filesize($file); 

header("Content-type:" . $mime_type); 
header("Content-Length:" . $filesize); 
header("Content-Disposition: attachment; filename=" . $file); 
header("Cache-Control: private, max-age=15"); 

@readfile($file); 

希望这会有所帮助。

+0

它不工作相同的输出即将到来。 –

+0

编辑我的答案。 –

+0

仍然无法正常工作,更新了我的问题。 –