好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php header函数下载文件实现代码 - php上传下载

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

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

header函数最常用的不是用于下载而是用于发送http类的跳转它会执行最后一个,不过是有条件的,

实例代码如下:

header('Location:http: //www.111cn.net");    header( 'Location:http://www.g.cn' );   header( 'Location:http://www.baidu.com' );  这个就会跳到百度  header( 'Location:http://www.111cn.net' ); echo  '烈火网;   header( 'Location:http://www.g.cn' );   header( 'Location:http://www.baidu.com' ); 

这个就会跳到google

发送状态

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

实例代码如下:

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

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

实例代码如下:

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

下载实例代码如下:

<?php   $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

查看更多关于php header函数下载文件实现代码 - php上传下载的详细内容...

  阅读:73次