脚本宝典收集整理的这篇文章主要介绍了PHP / Javascript:如何限制下载速度?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个PECL扩展,这使得这是一个非常简单的任务,称为pecl_http,其中包含函数http_throttle
.文档包含一个简单的例子,如何做到这一点.这个扩展还包含a HttpResponse
class,这是没有很好的文档的ATM,但我怀疑使用其setThroTTLeDelay玩和setBufferSize方法应该产生所需的结果(油门延迟=> 0.001,缓冲区大小20 ==〜20Kb /秒).从事物的外观,这应该是工作:
$download = new HttPResponse(); $download->setFile('yourFile.ext'); $download->setBufferSize(20); $download->setThrottleDelay(.001); //set headers using either the corresponding methods: $download->setContentType('application/octet-stream'); //or the setHeader method $download->setHeader('Content-Length',filesize('yourFile.ext')); $download->send();
$file = array( 'fname' => 'yourFile.ext','size' => filesize('yourFile.ext') ); header('Content-type: application/octet-stream'); header('Content-Description: file transfer'); header( sprintf( 'Content-Disposition: attachment; filename="%s"',$file['fname'] ) ); header('Content-Length: '. $file['size']); $open = fopen($file['fname'],"rb"); //handle error if (!$fh) while($chunk = fread($fh,2048))//read 2Kb { echo $chunk; usleep(100);//wait 1/10th of a second }
当然,不要缓冲输出,如果你这样做:),最好添加一个set_time_limit(0);声明也.如果文件很大,很可能您的脚本将在下载过程中遇害,因为它会触发最大执行时间.
> using NGINX
> using Apache2
> using MS IIS(要么安装比特率调节模块,要么设置最大带宽)
我从来没有限制自己的下载速度,但是看着这些链接,我认为很简单,说Nginx是最简单的:
location ^~ /downloadable/ { limit_rate_after 0m; limit_rate 20k; }
这使得速率限制立即启动,并将其设置为20k.详情可在the nginx wiki找到.
就apache而言,这并不是那么难,但是它需要你启用鼠标模块
LoadModule ratelimit_module modules/mod_ratelimit.so
<IfModule mod_ratelimit.c> <Location /downloadable> SetOutputFilter RATE_LIMIT Setenv rate-limit 20 </Location> </IfModule>
以上是脚本宝典为你收集整理的PHP / Javascript:如何限制下载速度?全部内容,希望文章能够帮你解决PHP / Javascript:如何限制下载速度?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。