我将所有文件存储在mysql数据库中作为blobs。我试图添加一个速度限制,用户可以通过我们的PHP网站下载它们的速度。我试过使用"sleep(1);"方法,它似乎不起作用,或者我做得不对。所以,如果有人知道限制速度的方法,我希望你的帮助。
这是我的下载代码
$query=mysql_query("SELECT * FROM file_servers WHERE id='$file_server_id'");
$fetch=mysql_fetch_assoc($query);
$file_server_ip=$fetch['ip'];
$file_server_port=$fetch['port'];
$file_server_username=$fetch['username'];
$file_server_password=$fetch['password'];
$file_server_db=$fetch['database_name'];
$connectto=$file_server_ip.":".$file_server_port;
if (!$linkid = @mysql_connect($connectto, $file_server_username, $file_server_password, true))
{
die("Unable to connect to storage server!");
}
if (!mysql_select_db($file_server_db, $linkid))
{
die("Unable to connect to storage database!");
}
$nodelist = array();
// Pull the list of file inodes
$SQL = "SELECT id FROM file_data WHERE file_id='$file_id' order by id";
if (!$RES = mysql_query($SQL, $linkid))
{
die("Failure to retrive list of file inodes");
}
while ($CUR = mysql_fetch_object($RES))
{
$nodelist[] = $CUR->id;
}
// Send down the header to the client
header("Content-Type: $data_type");
header("Content-Length: $size");
header("Content-Disposition: attachment; filename=$name");
// Loop thru and stream the nodes 1 by 1
for ($Z = 0 ; $Z < count($nodelist) ; $Z++)
{
$SQL = "select file_data from file_data where id = " . $nodelist[$Z];
if (!$RESX = mysql_query($SQL, $linkid))
{
die("Failure to retrive file node data");
}
$DataObj = mysql_fetch_object($RESX);
echo $DataObj->file_data;
}
一种方法是将冲洗和睡眠结合使用:
- 读取从数据库中得到的部分信息
- 输出一些字节
- 将输出刷新给用户
- 休眠1秒
但也采取掠夺油门功能:http://php.net/manual/en/function.http-throttle.php这里也有一个例子。我觉得这样更合适。
它位于您希望实现节流的代码的最后回显行中。我不太清楚php是否支持节流输出。
如果没有,您可以尝试拆分您希望回显的内容($DataObj->file_data
),并在
,并确保禁用输出缓冲。否则,直到整个脚本完成后,才会输出所有回显的内容。