下载文件几种实现方式

header实现文件下载

(代码摘录地址: https://www.cnblogs.com/dreamysky/p/5905882.html

function download($file){
    //文件根路径
    $filename=$_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'.$file;
    //下载文件
    if(!file_exists($filename)){
        $this->error("找不到文件");
        exit;
    }else{
        header("Content-Type:text/html;charset=utf-8");
        header("Content-type:application/force-download");
        header("Content-Type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Content-Length:".filesize($filename));//指定下载文件的大小
        header('Content-Disposition:attachment;filename="'.$file.'"');
        //必要时清除缓存
        ob_clean();
        flush();
        readfile($filename);
        exit();
    }
}
原文地址:https://www.cnblogs.com/gyrgyr/p/9947617.html