PHP头条
热点:

关于使用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服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。  
    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

www.phpzy.comtrue/phprm/15676.htmlTechArticle关于使用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...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐