pdf文件未下载

时间:2013-02-07 06:11:15

标签: php

我正在使用以下代码开始下载,但下载弹出窗口没有打开。相反,它显示该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.');
        }

1 个答案:

答案 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 );

希望这有帮助。