Header实现文件下载
1 function download($file){ 2 //文件根路径 3 $filename=$_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'.$file; 4 //下载文件 5 if(!file_exists($filename)){ 6 $this->error("找不到文件"); 7 exit; 8 }else{ 9 header("Content-Type:text/html;charset=utf-8"); 10 header("Content-type:application/force-download"); 11 header("Content-Type:application/octet-stream"); 13 header("Accept-Ranges:bytes"); 14 header("Content-Length:".filesize($filename));//指定下载文件的大小 15 header('Content-Disposition:attachment;filename="'.$file.'"'); 16 //必要时清除缓存 17 ob_clean(); 18 flush(); 19 readfile($filename); 20 exit(); 21 } 22 }
简单的实现header头进行文件下载,在lnmp环境下的项目,有时需要ob_clean()清除缓存一下,在flush()刷新缓存区;