Home >Backend Development >PHP Tutorial >PHP file download code (compatible with multiple browsers, supports Chinese file names)

PHP file download code (compatible with multiple browsers, supports Chinese file names)

WBOY
WBOYOriginal
2016-07-25 08:55:511079browse
  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";/ /Pay attention to 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 'rnYou The downloaded file is provided by the Programmer's Home Editorial Department! Thank you for your download. ';
  28. exit;
  29. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:an ordinary personNext article:an ordinary person