龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

PHP 强制下载类型

时间:2014-07-22 14:52来源: 作者: 点击:
分享到:
有时需要一些文件不能在线打开,而是执行后需要下载,这时就用这个函数就可以解决了
有时需要一些文件不能在线打开,而是执行后需要下载,这时就用这个函数就可以解决了
function downloadFile($file){
/*Coded by Alessio Delmonti*/        
        $file_name = $file;
        $mime = 'application/force-download';
        header('Pragma: public');       // required
        header('Expires: 0');           // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: close');
        readfile($file_name);           // push it out
        exit();
}
精彩图集

赞助商链接