<?php
$file = './中文.rar';
//EFIXME 有空格的時候basename只能獲取空格後半部分('/tmp/中文的 文檔.rar'獲得的是'文檔.rar','中文.rar'獲得的是'.rar')
$filename = basename($file);
header('Content-type: application/octet-stream');
// 處理中文文件名
$ua = $_SERVER['HTTP_USER_AGENT'];
$encoded_filename = urlencode($filename);
$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''" . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header("Content-Length: ". filesize($file));
//readfile($file);
// 只能在Apache中使用,且需要開啟mod_xsendfile
header("X-Sendfile: {$file}");