Skip to content

Instantly share code, notes, and snippets.

@foxwoods
Last active January 1, 2016 13:19
Show Gist options
  • Save foxwoods/8150025 to your computer and use it in GitHub Desktop.
Save foxwoods/8150025 to your computer and use it in GitHub Desktop.
通过设置正确的HTTP header,解决下载文件时Unicode文件名乱码的问题。Set proper header to make Unicode filename works in major browsers.
<?php
//
// Set proper header to make Unicode filename works in major browsers.
// see http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http
//
$filename = '一个文件.txt';
$ua = $_SERVER["HTTP_USER_AGENT"];
if(preg_match('/MSIE (?:6.0|7.0|8.0)/', $ua)) {
header('Content-Disposition: attachment; filename="'.rawurlencode($filename).'"');
} else {
header('Content-Disposition: attachment; filename*=UTF-8\'\''.rawurlencode($filename));
}
header('X-Content-Type-Options: nosniff');
header('Cache-Control: max-age=0');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment