php实现中文文件名文件下载

有很多朋友下载文件时都是把中文名转换在英文或全数字的然后再进行下载,下面我来介绍直接利用中文文件名文件下载并且不出现乱码的解决办法,代码如下:

  1. <?php 
  2. $filename = "中文 文件名.txt"
  3. header('Content-Type: application/octet-stream'); 
  4. header('Content-Disposition: attachment; filename=' . $filename); 
  5. print "Hello!"
  6. ?> 

把程序保存成UTF-8编码再访问,IE6下载的文件名就会乱码,FF3下下载的文件名就只有“中文”两个字,Opera 9下一切正常,输出的header实际上是这样子:

Content-Disposition: attachment; filename=中文 文件名.txt

其实按照RFC2231的定义,多语言编码的Content-Disposition,应该这么定义:

Content-Disposition: attachment; filename*="utf8''%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt"

即:filename后面的等号之前要加 * ,filename的值用单引号分成三段,分别是字符集(utf8)、语言(空)和urlencode过的文件名,最好加上双引号,否则文件名中空格后面的部分在Firefox中显示不出来.

注意:urlencode的结果与php的urlencode函数结果不太相同,php的urlencode会把空格替换成+,而这里需要替换成%20

实例代码如下:

  1. <?php 
  2.     $file = "/tmp/中文名.tar.gz"
  3.     $filename = basename($file); 
  4.     header("Content-type: application/octet-stream"); 
  5.     //处理中文文件名 
  6.     $ua = $_SERVER["HTTP_USER_AGENT"]; 
  7.     $encoded_filename = urlencode($filename); 
  8.     $encoded_filename = str_replace("+""%20"$encoded_filename); 
  9.     if (preg_match("/MSIE/"$ua)) { 
  10.      header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); 
  11.     } else if (preg_match("/Firefox/"$ua)) { 
  12.      header("Content-Disposition: attachment; filename*="utf8''" . $filename . '"'); 
  13.     } else { 
  14.      header('Content-Disposition: attachment; filename="' . $filename . '"'); 
  15.     } 
  16.     header('Content-Disposition: attachment; filename="' . $filename . '"'); 
  17.     header("Content-Length: "filesize($file)); 
  18.     readfile($file); 
  19. ?> 

这样我们就完全解决了中文文名乱码这个问题了。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php实现中文文件名文件下载

82 评论

  1. order avodart without prescription buy avodart generic buy ondansetron 8mg online

  2. order buspirone 10mg online cheap ditropan 5mg pill oxybutynin 2.5mg pill

  3. order imuran 100 mcg protonix ca order sildenafil sale

  4. buy tadalafil pill generic cialis order amantadine generic

  5. avlosulfon online aceon canada buy perindopril for sale

  6. clomid 100mg without prescription real online casino roulette free play for fun

  7. cialis tadalafil 40mg cialis drug oral viagra 100mg

  8. losartan 25mg canada buy cozaar sale topiramate 100mg oral

  9. buy tadalafil 40mg pill brand tadalafil buy viagra online cheap

  10. buy tadalafil 20mg pill order voltaren pill order indomethacin 50mg online cheap

  11. buy lamisil 250mg for sale lamisil drug buy trimox online

  12. cialis 40mg cheap Best price cialis play blackjack online for real money

  13. buy doxycycline without prescription buy doxycycline 100mg cleocin 150mg over the counter

  14. purchase temovate without prescription buspar 5mg pills amiodarone online order

  15. order diamox pills brand imdur 20mg buy azathioprine 50mg online

  16. buy ivermectin 3 mg for humans buy ed pills cheap order deltasone 10mg pills

  17. dutasteride sale avodart pills xenical 120mg tablet

  18. ditropan 5mg cheap cheap trileptal buy oxcarbazepine 300mg generic

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡