如何限制来自PHP脚本的传出响应速度? [英] How to limit speed of outgoing response from php script?

查看:132
本文介绍了如何限制来自PHP脚本的传出响应速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制来自php脚本的传出响应速度?因此,我有一个脚本在保持活动连接中生成数据.它只是打开文件并读取它.如何限制传出速度

How to limit speed of outgoing response from php script? So I have a script generating data in keep-alive connection. It just opens file and reads it. How to limit outgoing speed

(现在我有这样的代码)

(By now i have such code)

if(isset($_GET[FILE]))
 {
  $fileName = $_GET[FILE];
  $file =  $fileName;

  if (!file_exists($file))
  {
   print('<b>ERROR:</b> php could not find (' . $fileName . ') please check your settings.'); 
   exit();
  }
  if(file_exists($file))
  {
   # stay clean
   @ob_end_clean();
   @set_time_limit(0);

   # keep binary data safe
   set_magic_quotes_runtime(0);

   $fh = fopen($file, 'rb') or die ('<b>ERROR:</b> php could not open (' . $fileName . ')');
   # content headers
   header("Content-Type: video/x-flv"); 

   # output file
   while(!feof($fh)) 
   {
     # output file without bandwidth limiting
     print(fread($fh, filesize($file))); 
   } 
  } 
 }

那我该怎么做才能限制响应速度(例如限制为50 kb/s)

So what shall I do to limit speed of response (limit to for example 50 kb/s)

推荐答案

将文件输出更改为交错,而不是一次性输出整个文件.

Change your file output to be staggered rather that outputting the whole file in one go.

# output file
while(!feof($fh)) 
{
    # output file without bandwidth limiting
    print(fread($fh, 51200)); # 51200 bytes = 50 kB
    sleep(1);
}

这将输出50kB,然后等待一秒钟,直到输出整个文件.它应该将带宽限制为大约50kB/秒.

This will output 50kB then wait one second until the whole file is output. It should cap the bandwidth to around 50kB/second.

即使在PHP中可以做到这一点,我也会使用您的网络服务器来控制节流.

Even though this is possible within PHP, I'd use your web-server to control the throttling.

这篇关于如何限制来自PHP脚本的传出响应速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆