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

 

相关文章:

  • 2021-05-23
  • 2022-12-23
  • 2021-09-08
  • 2021-11-11
  • 2021-09-18
猜你喜欢
  • 2021-09-08
  • 2021-12-19
  • 2021-09-01
  • 2022-12-23
  • 2021-11-15
  • 2022-12-23
  • 2021-12-06
相关资源
相似解决方案