Home > Backend Development > PHP Tutorial > Sample code for php file download (preventing garbled Chinese file names)

Sample code for php file download (preventing garbled Chinese file names)

WBOY
Release: 2016-07-25 08:55:41
Original
1047 people have browsed it
  1. /**
  2. * PHP file download code, Chinese without garbled characters
  3. * by bbs.it-home.org
  4. */
  5. $file = "/tmp/中文名.tar.gz";
  6. $filename = basename($file);
  7. header(" Content-type: application/octet-stream");
  8. //Processing Chinese file names
  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);
Copy code

In addition, interested friends can study the module mod_xsendfile module in the apache server, which can be used It improves the efficiency of PHP sending files.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template