php下载代码

<?php
 $filename=$_SERVER['DOCUMENT_ROOT']."/test/file/20130503/20130503150036_85945.doc";
 $title="下载测试9";
    
 down($filename,$title);
    
 //$filename 必须是路径,不是url
 function down($filename,$title=''){
     $file = fopen($filename,"r");
     $filesize = filesize($filename);
     $encoded_filename = urlencode($title);
     $encoded_filename = str_replace("+", "%20", $title);
     $ua = $_SERVER["HTTP_USER_AGENT"];
     if (preg_match("/MSIE/is", $ua)) { 
         $file_name = urlencode($title);
         header("Pragma: public"); header("Expires: 0");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         header("Content-Type: application/force-download");
         header('Content-Type: application/vnd.ms-excel; charset=utf-8');
         header("Content-Transfer-Encoding: binary");
         header('Content-Disposition: attachment; filename='.$file_name);
     } else {
         header('Content-Type: application/octet-stream');
         if (preg_match("/Firefox/is", $ua)) { 
             header('Content-Disposition: attachment; filename*="utf8\'\'' . $title . '"');
         } else { 
             header('Content-Disposition: attachment; filename="' . $title . '"');
         }
     }
     echo fread($file, $filesize);
     fclose($file);
 }


知识共享许可协议
《php下载代码》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     184.25ms