Home > Backend Development > PHP Tutorial > Solution to the problem of Chinese garbled characters in PHP download file names under cross-browser

Solution to the problem of Chinese garbled characters in PHP download file names under cross-browser

WBOY
Release: 2016-07-25 09:03:01
Original
1249 people have browsed it
  1. $ua = $_SERVER["HTTP_USER_AGENT"];

  2. $filename = "Chinese file name.txt";

  3. $encoded_filename = urlencode($filename);
  4. $encoded_filename = str_replace("+", "%20", $encoded_filename);

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

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

  7. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  8. } else if (preg_match("/Firefox/", $ua)) {
  9. header('Content-Disposition: attachment; filename*="utf8''' . $filename . '"');
  10. } else {
  11. header ('Content-Disposition: attachment; filename="' . $filename . '"');
  12. }

  13. print 'ABC';

  14. ?>
Copy code

The above is a more general solution (it is said that xp+IE7 will have problems, but it has not been verified).

This problem was encountered when using CI-Excel-Generation-Library. The solution:

  1. private function set_headers() {

  2. $ua = $_SERVER["HTTP_USER_AGENT"];
  3. $filename = $this->filename . ".xls";
  4. $encoded_filename = urlencode($filename);
  5. $encoded_filename = str_replace("+", "%20", $encoded_filename);

  6. header("Pragma: public");

  7. header ("Expires: 0");
  8. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  9. header("Content-Type: application/force-download");
  10. header("Content-Type: application/octet-stream");

  11. //header("Content-Type: application/vnd.ms-excel;charset=UTF-8") ;

  12. header("Content-Type: application/download");;
  13. if (preg_match("/MSIE/", $ua)) {
  14. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  15. } else if (preg_match("/Firefox/", $ua)) {
  16. header('Content-Disposition: attachment; filename*="utf8''' . $filename . '"');
  17. } else {
  18. header('Content-Disposition: attachment; filename="' . $filename . '"');
  19. }
  20. header("Content-Transfer-Encoding: binary ");
  21. }
  22. ?>< ;/p>
Copy code


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