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

WBOY
Lepaskan: 2016-07-25 09:03:01
asal
1216 orang telah melayarinya
  1. $ua = $_SERVER["HTTP_USER_AGENT"];

  2. $filename = "中文 文件名.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. ?>
复制代码

上面是一个比较通用的解决方案(据说xp+IE7会有问题,未验证)。

这个问题是在使用CI-Excel-Generation-Library时遇到的,解决办法:

  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. ?>
复制代码


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan