开发者

PHP Real-Time Bandwidth

I am trying to create a simple file hosting script. I'm in the process of learning PHP myself, thus facing some difficulties..

The idea is to have each user's account to have their own bandwidth allocation (for downloading).

I'm not sure how this is done. I've don开发者_如何学编程e some searching, but I cannot find my answer. As far as I know there are two ways to do it,

  1. Find out the file size downloaded, make an assumption that the file should be completed, add it to bandwidth used.

The problem with this is if the file has been partially downloaded, it would provide inaccurate result.

  1. Find out the bytes transfered.

I've no idea how this is done... Buffering?

A good example of what I'm trying to do is something like Rapidshare. They have a really accurate method of calculating how much bandwidth was used.

Please enlighten me with some possible methods to do this.


For bandwidth limiting the size of the resource is irelevant. You could use an algorithm like Token Bucket. I've put everything into a library for you: bandwidth-throttle/bandwidth-throttle. The tricky part is the shared token bucket, as you want the limit per user.:

use bandwidthThrottle\BandwidthThrottle;
use bandwidthThrottle\tokenBucket\storage\PDOStorage;

$in  = fopen(__DIR__ . "/resources/video.mpg", "r");
$out = fopen("php://output", "w");

$throttle = new BandwidthThrottle();
$throttle->setRate(100, BandwidthThrottle::KIBIBYTES); // Set limit to 100KiB/s
$throttle->setStorage(new PDOStorage($userId, $pdo)); // Set the limit per user
$throttle->throttle($out);

stream_copy_to_stream($in, $out);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜