PHP sample code for file downloading (preventing garbled Chinese file names)

Source: Internet
Author: User
Tags php sample code
PHP sample code for file downloading (preventing garbled Chinese file names)

  1. /**
  2. * PHP file download code, without garbled Chinese characters
  3. * By bbs.it-home.org
  4. */
  5. $ File = "/tmp/Chinese name .tar.gz ";
  6. $ Filename = basename ($ file );
  7. Header ("Content-type: application/octet-stream ");
  8. // Process the Chinese file name
  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 );

In addition, if you are interested, you can study the module mod_xsendfile module on the apache server and use it to improve the efficiency of PHP file sending.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.