Home > Backend Development > PHP Tutorial > Code for php to automatically generate TXT files

Code for php to automatically generate TXT files

WBOY
Release: 2016-07-25 09:04:05
Original
1593 people have browsed it
  1. $ua = $_SERVER["HTTP_USER_AGENT"];

  2. $filename = "Chinese file name.txt";
  3. $encoded_filename = urlencode($filename);
  4. $ encoded_filename = str_replace("+", "%20", $encoded_filename);

  5. $ua = $_SERVER["HTTP_USER_AGENT"];

  6. $filename = "Chinese file name.txt";
  7. $encoded_filename = urlencode($filename);
  8. $encoded_filename = str_replace("+", "%20", $encoded_filename);
  9. ?>

Copy code

The above code can support downloading Chinese file names.

2. Generate TXT file

  1. header("Content-Type: application/octet-stream"); ​​
  2. if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
  3. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  4. } elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
  5. header( 'Content-Disposition: attachment; filename*="utf8' . $filename . '"');
  6. } else {
  7. header('Content-Disposition: attachment; filename="' . $filename . '"');
  8. }

  9. header("Content-Type: application/octet-stream"); ​​

  10. if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
  11. header( 'Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  12. } elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
  13. header('Content-Disposition: attachment; filename*="utf8' . $filename . '"');
  14. } else {
  15. header('Content-Disposition: attachment; filename="' . $filename . '"');
  16. }//Compatible with all kind of browser.
  17. ?>

Copy the code

3. Output content This step is much simpler. Use echo to output directly, and "rn" is used to break the line. The output content is the content in txt. This function can also generate doc files. Just change the suffix to .doc. However, this method can only generate simple doc files. If there are pictures or links, you will have to find another way. Scripting School, I wish you a happy study.



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