解决Content-Disposition:|解决Content-Disposition: 下载中文名乱码,下载乱码

【解决Content-Disposition:|解决Content-Disposition: 下载中文名乱码,下载乱码】header('Content-Disposition: attachment; filename*="'. $FileNname . '"'); 浏览器的不同导致PHP下载中文名乱码。
解决办法如下:
经测试,目前以兼容各主流浏览器。


  1. $lb_sourcefile_name=$_GET['sourcefile'];
  2. $down_url=$_GET['operate'];
  3. if($_GET['mykey']){
  4. $mykey=$_GET['mykey'];
  5. $mykey=str_replace("_","+",$mykey);
  6. $mykey=base64_decode($mykey);
  7. if($mykey === '2013abcdefg2013lubansoft9527'){
  8. $lb_sourcefile_name=str_replace("_","+",$lb_sourcefile_name);
  9. $lb_sourcefile_name=base64_decode($lb_sourcefile_name);
  10. $down_url=str_replace("_","+",$down_url);
  11. $down_url=base64_decode($down_url);
  12. $down_url='../lb/rename_file/'.$down_url;
  13. $ua = $_SERVER["HTTP_USER_AGENT"];
  14. header('Content-Description: File Transfer');
  15. header('Content-Type: application/octet-stream');
  16. if (preg_match("/MSIE/", $ua)) {
  17. $lb_sourcefile_name = urlencode($lb_sourcefile_name);
  18. $lb_sourcefile_name = str_replace("+", "%20", $lb_sourcefile_name);
  19. header('Content-Disposition: attachment; filename="' . $lb_sourcefile_name . '"');
  20. }
  21. elseif (preg_match("/Firefox/", $ua)) {
  22. header('Content-Disposition: attachment; filename*="utf8\'\'' . $lb_sourcefile_name . '"');
  23. }
  24. else {
  25. header('Content-Disposition: attachment; filename="' . $lb_sourcefile_name . '"');
  26. }
  27. header('Content-Transfer-Encoding: binary');
  28. header('Expires: 0');
  29. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  30. header('Pragma: public');
  31. header('Content-Length: ' . filesize($down_url));
  32. ob_clean();
  33. flush();
  34. readfile($down_url);
  35. exit;
  36. }
  37. else{
  38. die(404);
  39. }
  40. }


本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1158319 ,如需转载请自行联系原作者




    推荐阅读