作为程序员一定要保持良好的睡眠,才能好编程

浏览器弹窗下载文件

发布时间:2018-04-08

劳而无功夺

程序很多时候,需要进行下载,那么如何使浏览器弹出窗口进行下载呢?


下面的代码详细的解释了:


header("Content-type:application/octet-stream");

header("Content-dispostion:attachment;filename:a.gif");

header("Content-Length:"filesize("a.gif"));


$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    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_clean();
    flush();
    readfile($file);
    exit;
}