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');
//这个就会跳到百度
发送状态
输出状态值到浏览器,主要用于访问权限控制
<?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
Content-disposition
是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer 接收到头时,它会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名。(请注意,这是设计导致的;无法使用此功能将文档保存到用户的计算机上,而不向用户询问保存位置。)
服务端向客户端游览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt、jpg等,会直接在浏览器中显示,如果需要提示用户保存,就要利用Content-Disposition进行一下处理,关键在于一定要加上attachment:
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.txt");
备注:这样浏览器会提示保存还是打开,即使选择打开,也会使用相关联的程序比如记事本打开,而不是IE直接打开了。
Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名。具体的定义如下:
content-disposition = “Content-Disposition” “:”
disposition-type *( “;” disposition-parm )
disposition-type = “attachment” | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = “filename” “=” quoted-string
disp-extension-token = token
disp-extension-parm = token “=” ( token | quoted-string )
那么由上可知具体的例子:
Content-Disposition: attachment; filename=“filename.xls”
当然filename参数可以包含路径信息,但User-Agnet会忽略掉这些信息,只会把路径信息的最后一部分做为文件名。当你在响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着你不想直接显示内容,而是弹出一个”文件下载”的对话框,接下来就是由你来决定“打开”还是“保存” 了。
注意事项:
1.当代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。 response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作。如下:
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);
不然会发现下载功能在opera和firefox里面好好的没问题,在IE下面就是不行,就是找不到文件。
php header函数下载文件实现代码的更多相关文章
- PHP——使用header()函数下载文件
思路:先指明内容的MIME类型,内容描述,内容长度(也即文件大小). 一.下载txt文件的程序: <?phpheader('Content-Type:text/plain');header('C ...
- php下载文件的代码示例
php下载文件的代码示例,需要的朋友可以参考下 <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content- ...
- php 利用header 函数 下载各种文件
http://www.php.net/manual/en/function.readfile.php <?php /** * 下载文件 * header函数 * */ dl_file($_GET ...
- header头 下载文件 参数详解
header( header( header( header( header( header( header( header( header( //2 浏览器不会响应缓存 //1 Public指示响应 ...
- 批量去除Teleport Pro整站下载文件冗余代码
teleport pro tppabs标签批量删除 teleport pro tppabs标签批量删除 使 用Teleport Pro下载的网页代码中包含了很多垃圾代码,比如下载的html网页代码中会 ...
- [转]最全的用正则批量去除Teleport Pro整站下载文件冗余代码
原文地址:http://www.jb51.net/article/43650.htm html原文件中tppabs标记是Teleport Pro软件留下的标记.该软件是离线浏览器,下载完整个网页后,它 ...
- Spring mvc下载文件java代码
/** * 下载模板文件 * @author cq */ @RequestMapping("/downloadExcel.do") public ResponseEntity< ...
- java 下载文件功能代码例子
public static void down(HttpServletRequest request, HttpServletResponse response) throws Exceptio ...
- php header函数实例代码
一个完美的演示PHP header()函数用法的完整代码. 其中介绍的refresh方法,比<META ……用起来更得心应手,应该是段不错的代码. <?php /*** Function ...
随机推荐
- 【Android】11.1 Activity的生命周期和管理
分类:C#.Android.VS2015: 创建日期:2016-02-21 一.基本概念 1.必须理解这些方法,否则你编的程序根本就没法在实际项目中使用 当然,如果仅仅是为了玩玩,或者仅仅是作为例子为 ...
- angular学习笔记(三)-视图绑定数据的两种方式
绑定数据有两种方式: <!DOCTYPE html> <html ng-app> <head> <title>2.2显示文本</title> ...
- SharePoint自动化系列——通过PowerShell创建SharePoint List Items
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 代码如下(保存到本地ps1文件中,右键run with PowerShell即可): Add-PS ...
- Android实例-红外线操作(XE10.2+小米5)
相关资料: http://blog.csdn.net/qq_21752153/article/details/50244717https://developer.xamarin.com/api/typ ...
- 起来吧!不要做奴隶的ITproject师们!
本文转自林忠信的博客: http://davylin.blog.163.com/blog/static/8138791201441465328380/ 起来吧! 不要做奴隶的ITproject师们! ...
- 【Unity笔记】屏幕坐标和鼠标坐标
屏幕坐标:左下角为原点. void Update(){ // 获取物体的屏幕坐标,世界坐标->屏幕坐标 Vector3 screenPos = Camera.main.WorldToScreen ...
- 初看mysql源代码之mysql.cc
在mysql的源代码目录中,sql目录是mysql源代码中经常变化的目录之一,也是MySQL服务器内核最为核心和重要的目录. sql目录除了包含mysqld.cc这一MySQL main函数所在文件外 ...
- C语言 · 淘淘的名单
算法提高 淘淘的名单 时间限制:100ms 内存限制:8.0MB 问题描述 by ZBY... :) 淘淘拿到了一份名单,他想对上面的名字进行处理,挑出一些特殊的名字,他请你来帮忙. ...
- [shell]shell脚本统计数值大小
#! /bin/bash array=( ... ) var1= var2= ;i<=;i++)); do array[i]="$( cat /sys/bus/iio/devices/ ...
- java定时任务Timer与ScheduledExecutorService<转>
在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现.下面LZ就其原理.实例以及Timer缺陷三个方面来解析java Timer定时器. ...