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

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

时间: 2024-08-02 08:04:39

php header函数下载文件实现代码的相关文章

php利用header函数下载各种文件_php实例

本文实例为大家分享了php header函数下载文件实现代码,供大家参考,具体内容如下 http://www.php.net/manual/en/function.readfile.php <?php /** * 下载文件 * header函数 * */ dl_file($_GET ['filename']); function dl_file($file) { $file = ".//images//" . $file; //First, see if the file exi

sql-Asp.net的上传和下载文件的代码怎么写?

问题描述 Asp.net的上传和下载文件的代码怎么写? 我要做一个Asp.netSql数据库的网盘系统,亲们快帮我想想办法啊 解决方案 参考:http://www.cnblogs.com/zcttxs/archive/2012/05/21/2511947.html

c++ http-如何通过C++编写通过http协议下载文件的代码?求求各位大神给点意见

问题描述 如何通过C++编写通过http协议下载文件的代码?求求各位大神给点意见 获取服务器文件列表............................................ 解决方案 源码有很多,测试着看看 参考:CSDN移动问答

asp.net下载文件程序代码

asp.net下载文件程序代码 System.IO.FileInfo file = new System.IO.FileInfo(pstrFilePath); if (file.Exists) { Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Disposition&

Php header()函数语法及使用代码_php技巧

语法: 复制代码 代码如下: Void header(string $string[,bool $replace=true [, int $http_response_code) 向客户端发送原始的HTTP报头需注意:Header函数必须在任何实际的输出前调用,无论是一般的html标签.文件中空行,或者来自php.就是在这个函数之前不能有任何形式的输出.参数说明: 参数 描述string 必需.规定要发送的报头字符串.replace 可选.指示该报头是否替换之前的报头,或添加第二个报头.默认是

php下载文件的代码示例_php实例

复制代码 代码如下: <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Tra

php中强制下载文件的代码(解决了IE下中文文件名乱码问题)_php技巧

中间遇到一个问题是提交的中文文件名直接放到header里在IE下会变成乱码,解决方法是将文件名先urlencode一下再放入header,如下. 复制代码 代码如下: <?php $file_name = urlencode($_REQUEST['filename']); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, po

php header函数下载中文文件名乱码(ie/chrome)

在下载文件中加入下段代码即可解决  代码如下 复制代码 $name = rawurlencode($filename); header("Content-type: text/plain; charset=utf-8"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header(&quo

Python实现多线程下载文件的代码实例_python

实现简单的多线程下载,需要关注如下几点:1.文件的大小:可以从reponse header中提取,如"Content-Length:911"表示大小是911字节2.任务拆分:指定各个线程下载的文件的哪一块,可以通过request header中添加"Range: bytes=300-400"(表示下载300~400byte的内容),注意可以请求的文件的range是[0, size-1]字节的.3.下载文件的聚合:各个线程将自己下载的文件块保存为临时文件,所有线程都完