基于php下載文件的詳解
有時候我們需要用php下載一些文件,一般就是為了隱藏文件的真實下載地址才需要這樣,否則這樣會增加服務器負擔,以下是小編整理的基于php下載文件的詳解,希望對大家有所幫助!
php下載文件,比如txt文件。
出現的效果就是,彈出瀏覽器自帶的下載框,出現另存為操作。有時候會出現內存溢出和超時的現象。
超時的話,設置set_time_limit(0);
出現內存溢出的話,有可能是因為從數據庫中取出的數據量太大導致的。
如果是從文件中讀取的話,出現內阿存溢出的話,就是代碼讀取方式不正確,調用files或者filegetcontens才會
如果是fopen的話,就給一個緩沖區,固定大小,讀入然后寫入,不會出現內存溢出的情況。
如代碼:
復制代碼 代碼如下:
if (file_exists($file_path)) { /pic/p>
$handle = fopen($file_path, "r");
while (!feof($handle)) {
$content = fgets($handle, 4096); /pic/p>
echo $content; /pic/pic/p>
}
fclose($handle);
}
但是在輸出之前,要調用一次,@ob_end_flush();不能循環調用,只調用一次就好。
@ob_end_flush();/pic/p>
文件下載:
content-type:/pic/p>
復制代碼 代碼如下:
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"); /pic/p>
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($filename). ' bytes');
Header('Content-Disposition: attachment; filename='.$filename); /pic/p>
【基于php下載文件的詳解】相關文章:
PHP配置文件詳解php.ini03-17
php實現簡單文件下載的方法10-08
php多個文件及圖片上傳實例詳解01-23
PHP中讀取大文件實現方法詳解11-23
PHP下載保存文件保存到本地的方法02-16
基于PHP+Ajax實現表單驗證的詳解01-01
為什么使用迅雷下載的文件都是PHP格式的12-31
PHP文件是什么 如何打開PHP文件03-06