php文件下载(防止中文文件名乱码)的示例代码

WBOY
풀어 주다: 2016-07-25 08:55:41
원래의
981명이 탐색했습니다.
  1. /**
  2. * php文件下载代码,中文无乱码
  3. * by bbs.it-home.org
  4. */
  5. $file = "/tmp/中文名.tar.gz";
  6. $filename = basename($file);
  7. header("Content-type: application/octet-stream");
  8. //处理中文文件名
  9. $ua = $_SERVER["HTTP_USER_AGENT"];
  10. $encoded_filename = urlencode($filename);
  11. $encoded_filename = str_replace("+", "%20", $encoded_filename);
  12. if (preg_match("/MSIE/", $ua)) {
  13. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  14. } else if (preg_match("/Firefox/", $ua)) {
  15. header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
  16. } else {
  17. header('Content-Disposition: attachment; filename="' . $filename . '"');
  18. }
  19. header('Content-Disposition: attachment; filename="' . $filename . '"');
  20. header("Content-Length: ". filesize($file));
  21. readfile($file);
复制代码

另外,有兴趣的朋友,可以研究下apache服务器中的module mod_xsendfile模块,可以用它来提高PHP发送文件的效率。



원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!