关于使用PHP向客户端发送文件-示例代码解释
[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开发 , php ,