当前位置:Gxlcms > PHP教程 > php跨浏览器下载文件时中文乱码问题的解决方法

php跨浏览器下载文件时中文乱码问题的解决方法

时间:2021-07-01 10:21:17 帮助过:26人阅读

  1. /**

  2. * 解决跨浏览器下载文件,中文乱码的问题
  3. * edit bbs.it-home.org
  4. */
  5. $ua = $_SERVER["HTTP_USER_AGENT"];

  6. $filename = "中文 文件名.txt";

  7. $encoded_filename = urlencode($filename);
  8. $encoded_filename = str_replace("+", "%20", $encoded_filename);

  9. header('Content-Type: application/octet-stream');

  10. if (preg_match("/MSIE/", $ua)) {

  11. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  12. } else if (preg_match("/Firefox/", $ua)) {
  13. header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
  14. } else {
  15. header('Content-Disposition: attachment; filename="' . $filename . '"');
  16. }

  17. print 'ABC';

  18. ?>

人气教程排行