1

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

1 回答 1

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

希望这可以帮助。

于 2013-02-07T06:20:27.987 回答