在php中檔案下載會利用到header fopen fread三個主要函數,同時還有一些輔助函數如判斷檔案存在file_exists is_file等函數,下面我們來看一款檔案下載可以限制下載速度執行個體
在php教程中檔案下載會利用到header fopen fread三個主要函數,同時還有一些輔助函數如判斷檔案存在file_exists is_file等函數,下面我們來看一款檔案下載可以限制下載速度執行個體
*/
$file = "test.mp3"; // file to be send to the client
$speed = 8.5; // 8,5 kb/s download rate limit
if(file_exists($file) && is_file($file)) {
header("cache-control: private");
header("content-type: application/octet-stream");
header("content-length: ".filesize($file));
header("content-disposition: filename=$file" . "%20");
flush();
$fd = fopen($file, "r");
while(!feof($fd)) {
echo fread($fd, round($speed*1024));
flush();
sleep(1);
}
fclose ($fd);
}
/*
flush
flush函數 重新整理php程式的緩衝 實現echo動態輸出
本函數實現的結果是頁面不斷地顯示echo輸出 的資料
for ($i=10; $i>0; $i--)
{
echo $i.'
';
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
sleep
sleep() 函數延遲代碼執行若干秒。
header
header() 函數向用戶端發送原始的 http 前序。
認識到一點很重要,即必須在任何實際的輸出被發送之前調用 header() 函數(在 php 4 以及更高的版本中,您可以使用輸出緩衝來解決此問題):
filesize 擷取檔案大小
fread 讀取由fopen開啟的檔案內容
http://www.bkjia.com/PHPjc/444821.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444821.htmlTechArticle在php中檔案下載會利用到header fopen fread三個主要函數,同時還有一些輔助函數如判斷檔案存在file_exists is_file等函數,下面我們來看一款檔案...