PHP automatically generate TXT file code

Source: Internet
Author: User
    1. $ua = $_server["Http_user_agent"];

    2. $filename = "Chinese filename. txt";
    3. $encoded _filename = UrlEncode ($filename);
    4. $encoded _filename = str_replace ("+", "%20", $encoded _filename);

    5. $ua = $_server["Http_user_agent"];

    6. $filename = "Chinese filename. txt";
    7. $encoded _filename = UrlEncode ($filename);
    8. $encoded _filename = str_replace ("+", "%20", $encoded _filename);
    9. ?>

Copy Code

The above code can support the download of 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 various browsers.
  17. ?>

Copy Code

3, the output of this step is much simpler, directly with the echo output, "\ r \ n" for the line. The content of the output is the content in txt. This feature can also generate a doc file, as long as the suffix is changed to. doc, but only a simple doc file can be generated in this way, and if there is a picture or link, you can only find another path. Scripting School, I wish you a pleasant study.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.