php檔下載(防止中文檔名亂碼)的範例程式碼

WBOY
發布: 2016-07-25 08:55:41
原創
972 人瀏覽過
  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學習者快速成長!