首頁  >  文章  >  後端開發  >  php跨瀏覽器下載檔案時中文亂碼問題的解決方法

php跨瀏覽器下載檔案時中文亂碼問題的解決方法

WBOY
WBOY原創
2016-07-25 08:58:531140瀏覽
  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. ?>

复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn