PHP使用header方式实现文件下载功能
author:一佰互联 2019-04-25   click:446

先给大家介绍下PHP header() 函数

定义和用法

header() 函数向客户端发送原始的 HTTP 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决此问题):

<html><?php// 结果出错// 在调用 header() 之前已存在输出header("Location: http://www.example.com/");?>

语法

header(string,replace,http_response_code)

描述
string 必需。规定要发送的报头字符串。
replace

可选。指示该报头是否替换之前的报头,或添加第二个报头。

默认是 true(替换)。false(允许相同类型的多个报头)。

http_response_code 可选。把 HTTP 响应代码强制为指定的值。(PHP 4 以及更高版本可用)

php文件下载可以使用http的请求头加上php的IO可以实现,很久之前写过这么一个功能,后来代码没了,今天记录一下

1、先看一下一个正常的http请求

HTTP/1.1 200 OKServer: TengineContent-Type: application/octet-streamContent-Length: 5050697Connection: keep-aliveDate: Thu, 12 Oct 2017 11:24:46 GMTAccept-Ranges: bytesContent-Disposition: attachment; filename=down/20170928/zjbb_2.9.5.apkExpires: Thu, 12 Oct 2017 11:25:46 GMTCache-Control: max-age=60Via: cache25.l2eu6-1[0,200-0,H], cache16.l2eu6-1[16,0], cache8.cn891[0,200-0,H], cache8.cn891[1,0]Age: 1733678X-Cache: HIT TCP_MEM_HIT dirn:6:277104755 mlen:-1X-Swift-SaveTime: Sat, 14 Oct 2017 00:50:47 GMTX-Swift-CacheTime: 93312000Timing-Allow-Origin: *EagleId: b73d0e1c15095411645886178e

2、一些常见的header功能

header("HTTP/1.1 200 OK"); // ok 正常访问header("HTTP/1.1 404 Not Found"); //通知浏览器 页面不存在header("HTTP/1.1 301 Moved Permanently"); //设置地址被永久的重定向 301header("Location: http://www.test.con/"); //跳转到一个新的地址header("Refresh: 10; url=http://www.test.con/"); //延迟转向 也就是隔几秒跳转header("X-Powered-By: PHP/7.0.0"); //修改 X-Powered-By信息header("Content-language: en"); //文档语言header("Content-Length: 1234"); //设置内容长度header("Last-Modified: ".gmdate("D, d M Y H:i:s", $time)." GMT"); //告诉浏览器最后一次修改时间header("HTTP/1.1 304 Not Modified"); //告诉浏览器文档内容没有发生改变###内容类型###header("Content-Type: text/html; charset=utf-8"); //网页编码header("Content-Type: text/plain"); //纯文本格式header("Content-Type: image/jpeg"); //JPG、JPEGheader("Content-Type: application/zip"); // ZIP文件header("Content-Type: application/pdf"); // PDF文件header("Content-Type: audio/mpeg"); // 音频文件header("Content-type: text/css"); //css文件header("Content-type: text/javascript"); //js文件header("Content-type: application/json"); //jsonheader("Content-type: application/pdf"); //pdfheader("Content-type: text/xml"); //xmlheader("Content-Type: application/x-shockw**e-flash"); //Flash动画#########声明一个下载的文件###header("Content-Type: application/octet-stream");header("Content-Disposition: attachment; filename="ITblog.zip"");header("Content-Transfer-Encoding: binary");readfile("test.zip");#########对当前文档禁用缓存###header("Cache-Control: no-cache, no-store, max-age=0, must-revalidate");header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");#########显示一个需要验证的登陆对话框###header("HTTP/1.1 401 Unauthorized");header("WWW-Authenticate: Basic realm="Top Secret"");#########声明一个需要下载的xls文件###header("Content-Disposition: attachment; filename=abc.xlsx");header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");header("Content-Length: ".filesize("./test.xls"));header("Content-Transfer-Encoding: binary");header("Cache-Control: must-revalidate");header("Pragma: public");readfile("./test.xls");

3、看下下载所要用的的请求头

header("Content-type:application/octet-stream");header("Accept-Ranges:bytes");header("Accept-Length:".$file_Size);header("Content-Disposition: attachment; filename=".$filename);
  • content-type:文件类型
  • Accept-Ranges:表示接收数据的类型或者范围,图片属于二进制的东西所以需要使用字节的方式传输
  • Accept-Length:表示接收的文件大小,php文件下载需要告诉浏览器下载的文件有多大
  • Content-Disposition:附件只需要把文件名给过去就可以,这个名称就是下载时显示的文件名称

4、php的文件操作出现的比较早,文件名是中文的时候需要注意转码

$filename=iconv("UTF-8","GB2312",$filename);

5、php的文件下载机制是首先nginx把文件信息读入服务器内存,然后使用请求头把文件二进制信息通过浏览器传给客户端

feof用来判断文件是否已经读到了末尾,fread用来把文件读入缓冲区,缓冲区的大小是1024,一边读取一边把数据输出到浏览器。为了下载的安全性每次读数据都进行字节的计数。文件读取完毕后关闭输入流

注意:

a、如果运行的过程中出现问题,可以清空(擦掉)输出缓冲区,使用下面的代码即可

ob_clean();

b、很多人喜欢用readfile,如果是大文件,可能会有问题

完整代码

<?php ob_clean(); $action = $_GET["action"]; $filename = base64_decode($action);//传的参数encode了 $filepath = "/data/www/www.test.com/".$filename; if(!file_exists($filepath)){  exit; } $fp=fopen($filepath,"r"); $filesize=filesize($filepath); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Accept-Length:".$filesize); header("Content-Disposition: attachment; filename=".$filename); $buffer=1024; $buffer_count=0; while(!feof($fp)&&$file_Size-$buffer_count>0){ $data=fread($fp,$buffer); $buffer_count+=$buffer;  echo $data; } fclose($fp);?>

PS:下面看一段实例代码php如何通过header文件头实现文件下载

具体代码如下所示:

$file = $_GET["file"];if(file_exists($file)){header("Content-type:application/octet-stream");$filename = basename($file);header("Content-Disposition:attachment;filename = ".$filename);header("Accept-ranges:bytes");header("Accept-length:".filesize($file));readfile($file);}else{  echo "<script>alert("文件不存在")</script>";}

总结

以上所述是小编给大家介绍的PHP使用header方式实现文件下载功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网页设计网站的支持!