I would do it this way. Output your files and update bandwith usage:

while($out=fread($file, 4096)) {
echo $out;
UPDATE bandwith_usage SET bandwith_usage = bandwith_usage+1 WHERE second = UNIX_TIMESTAMP() LIMIT 1
if(mysql_affected_rows()==0) { // if this is the first output in this second
INSERT bandwith_usage SET bandwith_usage = 1, second = UNIX_TIMESTAMP()
}
}


When you are about to start a download, check the bandwith usage first, in this case for the last minute:

SELECT AVG(bandwith_usage) usage FROM bandwith_usage WHERE second > UNIX_TIMESTAMP() - 60

usage * 4096 gives you usage in bytes per second. If it exceeds your limit, output a html page (sorry...), else open the file and start the above loop.

You might want to change 4096 to a higher value. You should also implement a garbage collector that will remove old bandwith usage data, the collector can make usage statistics too.

Alex wrote:
I'm considering start up a small download site with the large amount of excess bandwith I have on my site, however I thought it would be cool to put some sort of download queue up (you know, so all my bandwith doesn't get eaten up when 500 random people suddenly decide to download a 500meg file i've put up.

In any case, I'm once again to the point where I know what I want, but the logic of it escapes me. If anyone has ever done this before, seen the code behind this before, or knows basically how this would be done, I'd be more than happy if you would share your wisdom with me.


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to