php自動產生TXT檔案的代碼

來源:互聯網
上載者:User
  1. $ua = $_SERVER["HTTP_USER_AGENT"];

  2. $filename = "中文檔案名稱.txt";
  3. $encoded_filename = urlencode($filename);
  4. $encoded_filename = str_replace("+", "%20", $encoded_filename);

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

  6. $filename = "中文檔案名稱.txt";
  7. $encoded_filename = urlencode($filename);
  8. $encoded_filename = str_replace("+", "%20", $encoded_filename);
  9. ?>

複製代碼

以上代碼可支援下載中文檔案名稱。

2、產生TXT檔案

  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. }//相容各種瀏覽器。
  17. ?>

複製代碼

3、輸出內容這一步就簡單多了,直接用echo輸出,“\r\n”用以換行。輸出的內容即為txt裡的內容。此功能也可以產生doc檔案,只要把尾碼改成.doc即可,不過按這種方法只能產生簡單的doc檔案,如果有圖片或連結什麼的,則只能另尋他途了。指令碼學堂,祝您學習愉快。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.