2012-01-28 78 views
3

我有這裏的代碼,但我想限制用戶可以下載的速度,我將如何實現這個代碼;限制下載速度與PHP

header("Content-type: application/force-download"); 
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-length: ".filesize("uploads/$filename")); 
    header("Content-disposition: attachment; filename=\"$origname"); 
    readfile("uploads/$filename"); 

謝謝!

這是我試過的;

$download_rate = 100; 

$ origname = get_file_name($ file [0]);

header("Content-type: application/force-download"); 
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-length: ".filesize("uploads/$filename")); 
    header("Content-disposition: attachment; filename=\'$origname'"); 

    $local_file = "uploads/$origname"; 

//同花順內容 flush();

// open file stream 
$file = fopen($local_file, "r"); 

while (!feof($file)) { 

    // send the current file part to the browser 
    print fread($file, round($download_rate * 1024)); 

    // flush the content to the browser 
    flush(); 

    // sleep one second 
    sleep(1); 
} 

// close file stream 
fclose($file); 

爲什麼不工作?

回答

1

如果你已經嘗試過什麼@mugur建議則有可能是一個問題在別處。例如,在您的片段看來你已經有了一個缺少逃脫引號:

標題(「內容處置:附件;文件名= \」 $ origname「);

我想這應該是:

標題( 「內容處置:附件;文件名= \」 $ origname \ 「」);

1
+0

是啊我已經閱讀過這篇文章,但是我真的需要一種方法來整合它,因爲我已經嘗試過並失敗了! – HarryBeasant 2012-01-28 23:49:29

+2

好吧,你嘗試了什麼?它是如何失敗的?這似乎是一個很好的起點,所以不要要求所有人重做你已經完成的任務。 – Hamish 2012-01-28 23:52:23

+2

它會給出錯誤,下載速度更快還是失敗? – Mike 2012-01-28 23:52:24

0

在你的同時的fread,只是計算字節和叔imestamps。然後使用usleep添加一個微小的停頓。