溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP 中實現(xiàn)文件下載的幾種方法

發(fā)布時間:2020-06-18 17:29:03 來源:網絡 閱讀:470 作者:990653058 欄目:web開發(fā)

方法一:

    利用php中readfile函數(shù)讀取文件內容到輸出緩沖,然后再進行輸出,效率比較低,文件必須通過PHP進行一次處理才能進行輸出

<?php
 $file = 'test.zip';  
 header('content-type:application/octet-stream');  
 header('content-disposition:p_w_upload; filename='.basename($file));  
 header('content-length:'.filesize($file));  
 readfile($file);

方法二:

    這里和方法一是一樣的,只不過采用file_get_contents函數(shù)來讀取文件并輸出

<?php
 $file = 'test.zip';  
 header('content-type:application/octet-stream');  
 header('content-disposition:p_w_upload; filename='.basename($file));  
 header('content-length:'.filesize($file));  
 echo file_get_contents($file);

方法三:

    基于Apache的mod_xsendfile擴展,來進行文件的輸出,文件可以不經過php讀取到內存,這在處理大文件[超過1G的文件]方面是一個比較可取的選擇,首先可以檢查一下apache是否安裝改擴展:

rpm -q mod_xsendfile    --檢查是否安裝該擴展

yum install mod_xsendfile --執(zhí)行該命令安裝該擴展

service httpd restart    --安裝擴展之后重啟服務

httpd -M|grep xsendfile  --檢查apache是否成功加載該擴展

    安裝完該擴展之后,如需要在配置文件中添加該選項:

XSendFile On  --開啟該選項后,需要重啟服務

然后編輯一個php文件,測試是否能正常輸出:

 header('content-disposition:p_w_upload; filename=test.zip');
 header("X-Sendfile:/var/www/test.zip");

注意文件的權限問題

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI