PHP sample code for file downloading (preventing garbled Chinese file names)
- /**
- * PHP file download code, without garbled Chinese characters
- * By bbs.it-home.org
- */
- $ File = "/tmp/Chinese name .tar.gz ";
-
- $ Filename = basename ($ file );
-
- Header ("Content-type: application/octet-stream ");
-
- // Process the Chinese file name
- $ Ua = $ _ SERVER ["HTTP_USER_AGENT"];
- $ Encoded_filename = urlencode ($ filename );
- $ Encoded_filename = str_replace ("+", "% 20", $ encoded_filename );
- If (preg_match ("/MSIE/", $ ua )){
- Header ('content-Disposition: attachment; filename = "'. $ encoded_filename .'"');
- } Else if (preg_match ("/Firefox/", $ ua )){
- Header ("Content-Disposition: attachment; filename * = \" utf8'' ". $ filename .'"');
- } Else {
- Header ('content-Disposition: attachment; filename = "'. $ filename .'"');
- }
-
- Header ('content-Disposition: attachment; filename = "'. $ filename .'"');
- Header ("Content-Length:". filesize ($ file ));
- 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. |