PHP下载时,中文文件名在不同浏览器(IE/Chrome/Firefox)下,表现会不同,有的会出现乱码,解决办法如下

<?php

$ua = $_SERVER['HTTP_USER_AGENT'];
$filename_encoded = urlencode($filename);

header('Content-Type: application/octet-stream');
if(preg_match('/Firefox/', $ua)){
    header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename_encoded . '"');
}else{
    header("Content-Disposition: attachment; filename={$filename_encoded}");
}
header('Content-Length: '.strlen($content));
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $content;

Post Navigation