Header实现文件下载

Posted ゞ断桥边丿独醉秋ゞ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()刷新缓存区;

以上是关于Header实现文件下载的主要内容,如果未能解决你的问题,请参考以下文章

Vscode for python ide配置

Glob观看多个文件,处理一个

如何在使用Java DSL的camel Header中设置没有扩展名的文件名?

代码片段 navbar

head标签怎么给多个html引用

有人知道下面的代码片段是啥意思吗?