$download_rate = 20.5;if(file_exists($local_file) && is_file($local_file)) {
// send headers header('cache-control: private'); header('content-type: application/octet-stream'); header('content-length: '.filesize($local_file)); header('content-disposition: filename='.$download_file); // flush content
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);}else { die('error: the file '.$local_file.' does not exist!');}?>
复制代码
附:修改discuz论坛实现对附件下载的限速
研究了一下discuz的代码,实现了一个可行的方案。很简单,找到attachment.pnp,找到最下面,有个:function getlocalfile($filename, $readmod = 1, $range = 0)
修改为:
复制代码
经过测试,发现效果还是很明显的。