Generate txt and download automatically when you access PHP.
First step: Handle Chinese file name:
PHP code
- $ua = $_server["Http_user_agent"];
- $filename = "Chinese filename. txt";
- $encoded _filename = UrlEncode ($filename);
- $encoded _filename = Str_replace ("+", "%20", $encoded _filename);
The above method can support downloading Chinese file names.
Step two: Generate TXT file
PHP code
- Header ("Content-type:application/octet-stream");
- if (Preg_match ("/msie/", $_server[' http_user_agent '])) {
- Header (' content-disposition:attachment; Filename= '. $encoded _filename. "');
- } ElseIf (Preg_match ("/firefox/", $_server[' http_user_agent '])) {
- Header (' content-disposition:attachment; filename*= ' UTF8 '. $filename. "');
- } Else {
- Header (' content-disposition:attachment; Filename= '. $filename. "');
- }
supports a variety of browsers.
Step Two: Output content
Directly with the echo output, "\ r \ n" for line break.
PHP Download TXT file