简单的下载文件函数

jerry thinkphp 2015年11月18日 收藏
发送文件给浏览器,提供下载,支持个大浏览器及中文文件名。
  1. //下载文件
  2. function download_file($file){
  3.     if(is_file($file)){
  4.         $length = filesize($file);
  5.         $type = mime_content_type($file);
  6.         $showname =  ltrim(strrchr($file,'/'),'/');
  7.         header("Content-Description: File Transfer");
  8.         header('Content-type: ' . $type);
  9.         header('Content-Length:' . $length);
  10.          if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
  11.              header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
  12.          } else {
  13.              header('Content-Disposition: attachment; filename="' . $showname . '"');
  14.          }
  15.          readfile($file);
  16.          exit;
  17.      } else {
  18.          exit('文件已被删除!');
  19.      }
  20.  }