<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            php語言

            基于php下載文件的詳解

            時間:2025-02-03 00:37:05 php語言 我要投稿
            • 相關推薦

            基于php下載文件的詳解

              有時候我們需要用php下載一些文件,一般就是為了隱藏文件的真實下載地址才需要這樣,否則這樣會增加服務器負擔,以下是小編整理的基于php下載文件的詳解,希望對大家有所幫助!

              php下載文件,比如txt文件。

              出現的效果就是,彈出瀏覽器自帶的下載框,出現另存為操作。有時候會出現內存溢出和超時的現象。

              超時的話,設置set_time_limit(0);

              出現內存溢出的話,有可能是因為從數據庫中取出的數據量太大導致的。

              如果是從文件中讀取的話,出現內阿存溢出的話,就是代碼讀取方式不正確,調用files或者filegetcontens才會

              如果是fopen的話,就給一個緩沖區,固定大小,讀入然后寫入,不會出現內存溢出的情況。

              如代碼:

              復制代碼 代碼如下:

              if (file_exists($file_path)) { //如果文件存在

              $handle = fopen($file_path, "r");

              while (!feof($handle)) {

              $content = fgets($handle, 4096); //讀取一行

              echo $content; //輸出到緩沖區,即php://stdout。達到緩沖區設置值后由tcp傳給瀏覽器進行輸出  一般到512字節就會通過網絡輸出給瀏覽器

              }

              fclose($handle);

              }

              但是在輸出之前,要調用一次,@ob_end_flush();不能循環調用,只調用一次就好。

              @ob_end_flush();//沖刷出(送出)輸出緩沖區內容并關閉緩沖

              文件下載:

              content-type://下載的格式,瀏覽器不能解析的格式就會彈出下載框

              復制代碼 代碼如下:

              header("Content-Type: application/force-download");

              header("Content-Type: application/download");

              header("Content-Transfer-Encoding: binary");

              header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

              header("Pragma: no-cache");

              Header("Content-type: application/octet-stream");  //響應內容類型

              Header("Accept-Ranges: bytes");

              Header("Accept-Length: ".filesize($filename). ' bytes');

              Header('Content-Disposition: attachment; filename='.$filename);  //HTTP響應頭

            【基于php下載文件的詳解】相關文章:

            PHP配置文件詳解php.ini10-19

            php實現簡單文件下載的方法07-24

            php多個文件及圖片上傳實例詳解08-02

            PHP中讀取大文件實現方法詳解09-23

            PHP下載保存文件保存到本地的方法08-26

            基于PHP+Ajax實現表單驗證的詳解08-22

            為什么使用迅雷下載的文件都是PHP格式的09-15

            php readfile下載大文件失敗的解決方法06-13

            如何打開php文件 php文件怎么打開09-06

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看