当前位置:Gxlcms > PHP教程 > php文件下载代码(多浏览器兼容、支持中文文件名)

php文件下载代码(多浏览器兼容、支持中文文件名)

时间:2021-07-01 10:21:17 帮助过:26人阅读

  1. /**
  2. * php文件下载示例
  3. * by bbs.it-home.org
  4. */
  5. ob_start();
  6. $ua = $_SERVER["HTTP_USER_AGENT"];
  7. $filename ="脚本学堂_文件下载.doc";//注意转码
  8. $encoded_filename = urlencode($filename);
  9. //兼容各种浏览器
  10. $encoded_filename = str_replace("+", "%20", $encoded_filename);
  11. header('Content-Type: application/octet-stream');
  12. if (preg_match("/MSIE/", $ua))
  13. {
  14. header('Content-Disposition: attachment; filename="'.urldecode($encoded_filename).'"');
  15. }
  16. else if (preg_match("/Firefox/", $ua))
  17. {
  18. header('Content-Disposition: attachment; filename*="gbk\'\''.$filename.'"');
  19. }
  20. else
  21. {
  22. header('Content-Disposition: attachment; filename="'.$filename.'"');
  23. }
  24. ob_clean();
  25. flush();
  26. readfile($filename);
  27. echo '\r\n您下载的文件由程序员之家编辑部提供!感谢您的下载。';
  28. exit;
  29. ?>

人气教程排行