2015-05-27
[php]
<?php
function downloadFile( $fullPath ){
// Must be fresh start
if( headers_sent() ) //check if any header has been sent
die('Headers Sent'); //Equivalent to exit()
// Required for some browsers
if(ini_get('zlib.output_compression')) //Gets the value of a configuration option
ini_set('zlib.output_compression', 'Off'); //该模块允许PHP透明的读取和写入gzip(.gz)压缩文件
// File Exists?
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);//返回文件路径的信息
/*
$path_parts = pathinfo("/www/htdocs/index.html");
echo $path_parts["dirname"] . "/n";
echo $path_parts["basename"] . "/n";
echo $path_parts["extension"] . "/n";//后缀名
返回的信息分别为:
/www/htdocs
index.html
html
*/
$ext = strtolower($path_parts["extension"]); //将字符串转化为小写
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required 指明响应可被任何缓存保存
/*
The same that "Cache-Control: public"
public:指明响应可被任何缓存保存,即便该响应通常是不可缓存的或只在非共享缓存里是可缓存的。
private:指明响应消息的部分或所有部分是为一个用户准备的并且不得被共享缓存保存。可以使源服
务器可以声明响应的特定部分来针对某一用户并且对其他用户的请求是无效的。一个私有
(非共享)缓存可以缓存此响应。
no-cache:如果no-cache缓存控制指令没有指定一个field-name,那么一个缓存不能利用此响应在没
有通过源服务器对它进行成功重验证的情况下去满足后续的请求。这允许源服务器去防止响
应被缓存保存,即使此缓存已被设置可以返回陈旧响应给客户端。
如果no-cache缓存控制指令指定一个或多个field-name,那么缓存可以利用此响应去满足
后续的请求,但这要受限于对缓存的其它限制。然而,指定的filed-name必须不能在后续请
求的响应里被发送如果此响应没有在源服务器那里得到成功重验证。这允许源服务器能防止
缓存去重利用响应里的某些头域,但仍然允许缓存能保存响应剩余部分。
/*
The Pragma header specifies directives for proxy and gateway systems.
Since many proxy systems may exist between a client and server, Pragma
headers must pass through each proxy. When the Pragma header reaches
the server, the header may be ignored by the server software.
The only directive defined in HTTP/1.0 is the no-cache directive. It is
used to tell caching proxies to contact the server for the requested
document, instead of using its local cache. This allows the client to
request the most up-to-date document from the original web server,
without receiving a cached copy from an intermediate proxy server.
The Pragma header is an HTTP 1.0 feature, and is maintained in HTTP 1.1
for backward compatibility. No new Pragma directives will be defined in
the future.
*/
header("Expires: 0");
/*
Expires实体头域(entity-header)给出了在何时之后响应即被视为陈旧的。一个陈旧的缓存项
不能被缓存(一个代理缓存或一个用户代理的缓存)返回给客户端,除非此缓存项被源服务器
(或者被一个拥有实体的保鲜副本的中间缓存)验证。
0表示立即过期,就是不缓存的意思。
*/
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
/*
post-check and pre-check cache control directives must appear together
in pairs other wise they are ignored.
*/
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=/"".basename($fullPath)."/";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean(); //Clean (erase) the output buffer
flush(); //刷新PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。
readfile( $fullPath ); //读入一个文件并写入到输出缓冲。
} else
die('File Not Found');
}
?>
这是PHP手册上的示例代码,自己看不懂,就把一条条的翻译出来。
下面是
© kekehu / 技术资源 / 2009.02.17 / 10:15 / 4246PV
网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private、no-cache、max-age、must-revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:
(1) 打开新窗口
值为private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器。
而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:
Cache-control: max-age=5(表示当访问此网页后的5秒内再次访问不会去服务器)
(2) 在地址栏回车
值为private或must-revalidate则只有第一次访问时会访问服务器,以后就不再访问。
值为no-cache,那么每次都会访问。
值为max-age,则在过期之前不会重复访问。
(3) 按后退按扭
值为private、must-revalidate、max-age,则不会重访问,
值为no-cache,则每次都重复访问
(4) 按刷新按扭
无论为何值,都会重复访问
Cache-control值为“no-cache”时,访问此页面不会在Internet临时文章夹留下页面备份。
另外,通过指定“Expires”值也会影响到缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问: Expires: Fri, 31 Dec 1999 16:00:00 GMT
比如:禁止页面在IE中缓存
http响应消息头部设置:
CacheControl = no-cache
Pragma=no-cache
Expires = -1
Expires是个好东东,如果服务器上的网页经常变化,就把它设置为-1,表示立即过期。如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。
当HTTP1.1服务器指定CacheControl = no-cache时,浏览器就不会缓存该网页。
旧式 HTTP 1.0 服务器不能使用 Cache-Control 标题。
所以为了向后兼容 HTTP 1.0 服务器,IE使用Pragma:no-cache 标题对 HTTP 提供特殊支持。
如果客户端通过安全连接 (https://)与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,
则 Internet Explorer不会缓存此响应。注意:Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-1相同,该页将被缓存,但被标记为立即过期。
作者:wolinxuebin
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
6
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
PHP中include和require区别之我见
09-05
将视频设置为Android手机开机动画的教程
2014-12-11
常用dos命令及语法
2014-09-27
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
java中的info是什么意思
2022-03-24
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
学ug编程如何快速入门?
2022-03-17
PHP数组函数array
2014-09-05
火影忍者国际版手游下载v1.71.72.8 安卓最新版本
角色扮演 1.86G
下载绿茵信仰游戏最新版下载v3.8.0 安卓版
体育运动 1.80G
下载奇妙梦幻城堡完整版下载v9.87.00.00 安卓版
其它手游 105.1M
下载labo积木汽车2儿童游戏下载v1.1.422 安卓版
其它手游 131.5M
下载奇妙环游世界宝宝巴士游戏下载v9.87.00.00 安卓版
其它手游 114.2M
下载全民农场游戏下载v1.26.30 安卓正版
经营养成 887.0M
下载哈利波特魔法觉醒网易版下载v1.20.220280 安卓版
其它手游 1.89G
下载宝可梦大集结体验服免费下载v1.2.1.1 安卓手机内测服
其它手游 1.90G
下载宝可梦大集结国服下载v1.2.1.1 官方安卓手机版
下载
问道果盘版本渠道服下载v2.139.0506 安卓客户端
下载
斗破苍穹异火重燃官方腾讯版下载v0.0.0.352 安卓最新版
下载
战舰帝国折扣平台下载v7.1.86 安卓版
下载
宝可梦大集结腾讯版国服下载v1.3.1.1 安卓官方正版
下载
大帝国征服者手游百度版下载v5.670 安卓版
下载
漫威对决最新版下载v1.0.125569 安卓版
下载
雪糕工厂宝宝巴士游戏下载v9.87.13.01 官方安卓版
下载