Home  >  Article  >  Backend Development  >  Solution to the Chinese garbled problem when PHP cross-browser downloads files

Solution to the Chinese garbled problem when PHP cross-browser downloads files

WBOY
WBOYOriginal
2016-07-25 08:58:531140browse
  1. /**

  2. * Solve the problem of garbled Chinese characters when downloading files across browsers
  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. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn