public static function download($filename, $showname = '', $content = '', $expire = 180)
{
if (is_file($filename)) {
$length = filesize($filename);
} elseif (is_file(UPLOAD_PATH . $filename)) {
$filename = UPLOAD_PATH . $filename;
$length = filesize($filename);
} elseif ($content != '') {
$length = strlen($content);
} else {
throw_exception(L('PUBLIC_ATTACH_ISNULL'));
//throw_exception($filename.L('PUBLIC_ATTACH_ISNULL'));
}
if (empty($showname)) {
$showname = $filename;
}
//造成部分中文文件的乱码问题
//$showname = basename($showname);
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);
header('Content-Disposition: attachment; filename= ' . $showname . ' ');
header('Content-Encoding: none');
header('Content-Transfer-Encoding: binary');
if ($content == '') {
readfile($filename);
} else {
echo $content;
}
exit;
}