/** * 下载文件 * 可以指定下载显示的文件名,并自动发送相应的Header信息 * 如果指定了content参数,则下载该参数的内容 * @param string $filename 下载文件名 * @param string $showname 下载显示的文件名 * @param string $content 下载的内容 * @param integer $expire 下载内容浏览器缓存时间*/function download($filename, $showname = '', $content = '', $expire = 180) { if(is_file($filename)) { $length = filesize($filename); } else if($content != '') { $length = strlen($content); } else { die('下载文件不存在!'); } if(trim($showname) == "") { $showname = $filename; } //中文文件名不兼容修改 //$showname = basename($showname); $tmpA = explode('/', $showname); $showname = end($tmpA); if(!empty($filename)) { $type = mime_content_type($filename); } else { $type = "application/octet-stream"; } //发送Http Header信息 开始下载 header("Pragma: public"); header("Cache-control: max-age=".$expire); //header('Cache-Control: no-store, no-cache, must-revalidate'); header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expire) . "GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . "GMT"); header("Content-Length: " . $length); header("Content-type: " . $type.'; charset=utf-8'); header('Content-Encoding: none'); header("Content-Transfer-Encoding: binary" ); //解决各个浏览器下载文件名乱码问题 $ua = $_SERVER["HTTP_USER_AGENT"]; $encoded_filename = urlencode($showname); $encoded_filename = str_replace("+", "%20", $encoded_filename); if (preg_match("/MSIE/", $ua)) { header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); } else if (preg_match("/Firefox/", $ua)) { header('Content-Disposition: attachment; filename*="utf8\'\'' . $showname . '"'); } else { header('Content-Disposition: attachment; filename="' . $showname . '"'); } if($content == '' ) { readfile($filename); } else { echo($content); } exit();}