PHP File download code (multi-browser compatible, support Chinese file name)

Source: Internet
Author: User
  1. /**
  2. * PHP File Download example
  3. * by bbs.it-home.org
  4. */
  5. Ob_start ();
  6. $ua = $_server["Http_user_agent"];
  7. $filename = "Script Academy _ File download. doc";//Note transcoding
  8. $encoded _filename = UrlEncode ($filename);
  9. Compatible with various browsers
  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 Your downloaded file is provided by the programmer's Home editorial office! Thank you for your download. ';
  28. Exit
  29. ?>
Copy Code
  • 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.