朱清林's Blog

朱清林's Blog

解决PHP输出下载文件名乱码问题

$fileurl = "uploads/dms_data/excel/export/test.csv";
$file = fopen("uploads/dms_data/excel/export/test.csv","r"); // 打开文件 
$file_name = "设备信息导出 ";
$file_name .= date('Y-m-d H-i-s',time());
$file_name = "设备信息导出 ";
$file_name .= date('Y-m-d H-i-s',time());

header("Content-type: application/octet-stream"); 
header("Accept-Ranges: bytes"); 
header("Accept-Length: ".filesize($fileurl));

$ua = $_SERVER["HTTP_USER_AGENT"]; 
if(preg_match("/Firefox/", $ua)){ //火狐浏览器需要特殊处理
	header('Content-Disposition: attachment; filename*="utf8\'\'' . $file_name . '.csv"');
}else{
	header('Content-Disposition: attachment; filename='. urlencode($file_name . ".csv"));
} 
// 输出文件内容 
echo fread($file,filesize($fileurl)); 
fclose($file); 

 

原文地址:http://www.lltonet.com/3.html

阅读 681   190