Heim  >  Artikel  >  Backend-Entwicklung  >  php header函数下载文件实现代码_PHP教程

php header函数下载文件实现代码_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:15:17845Durchsuche

在php中header函数的使用很大,header不但可以向客户端发送原始的 HTTP 报头信息,同时还可以直接实现文件下载操作,接下来小编给大家介绍欢迎各位朋友参考。

header函数最常用的不是用于下载而是用于发送http类的

跳转

它会执行最后一个,不过是有条件的,例如:

 代码如下 复制代码

header('Location:http://www.bKjia.c0m");
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');

这个就会跳到百度

header('Location:http://www.bKjia.c0m');echo '帮客之家;
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');

这个就会跳到google


发送状态

输出状态值到浏览器,主要用于访问权限控制

 代码如下 复制代码

header('HTTP/1.1 401 Unauthorized');
header('status: 401 Unauthorized');
?>

比如要限制一个用户不能访问该页,则可设置状态为404,如下所示,这样浏览器就显示为即该页不存在

 代码如下 复制代码

header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
?>

下载


 代码如下 复制代码

$filename = '路径+实际文件名';
//文件的类型
header('Content-type: application/pdf');
//下载显示的名字
header('Content-Disposition: attachment; filename="保存时的文件名.pdf"');
readfile("$filename");
exit();
?>

header函数进行相应的转化,

 代码如下 复制代码
header(‘Content-type: application/octet-stream’);//输出的类型,根据下面提供的MIME表,选择相应的类型
header(‘Content-Disposition: attachment; filename=”下载显示名字.rar”‘);//下载显示的名字
readfile(‘服务器上的文件名.rar’);//

要下的文件,包括路径

常用的MIME类型
.doc    application/msword
.docx   application/vnd.openxmlformats-officedocument.wordprocessingml.document
.rtf    application/rtf
.xls    application/vnd.ms-excel application/x-excel
.xlsx   application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.ppt    application/vnd.ms-powerpoint
.pptx   application/vnd.openxmlformats-officedocument.presentationml.presentation
.pps   application/vnd.ms-powerpoint
.ppsx  application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pdf   application/pdf
.swf   application/x-shockwave-flash
.dll   application/x-msdownload
.exe   application/octet-stream
.msi   application/octet-stream
.chm   application/octet-stream
.cab   application/octet-stream
.ocx   application/octet-stream
.rar  application/octet-stream
.tar  application/x-tar
.tgz  application/x-compressed
.zip  application/x-zip-compressed
.z    application/x-compress
.wav   audio/wav
.wma   audio/x-ms-wma
.wmv   video/x-ms-wmv
.mp3 .mp2 .mpe .mpeg .mpg   audio/mpeg
.rm   application/vnd.rn-realmedia
.mid .midi .rmi   audio/mid
.bmp   image/bmp
.gif   image/gif
.png   image/png
.tif .tiff    image/tiff
.jpe .jpeg .jpg    image/jpeg
.txt  text/plain
.xml  text/xml
.html text/html
.css  text/css
.js   text/javascript

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/628850.htmlTechArticle在php中header函数的使用很大,header不但可以向客户端发送原始的 HTTP 报头信息,同时还可以直接实现文件下载操作,接下来小编给大家介绍...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn