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

WBOY
Release: 2016-07-25 08:58:53
Original
1159 people have browsed it
  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. ?>

复制代码


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!