user1934039
user1934039

Reputation: 23

Restrictions on file download speed?

I am contemplating setting up a file host (mostly for the exercise) but how do you ensure that free users are only capable of 40-50 kb/s speed while premium users can go at faster speeds?

I guess you place all the files on 2 separate servers and simply control the port connection (10 Mbit vs. 1000 Mbit), but that would require a mirror harddisk setup.

With all the file hosts out there, I am betting there must be a simpler solution.

Upvotes: 2

Views: 161

Answers (2)

Markus Malkusch
Markus Malkusch

Reputation: 7868

You can directly control the bandwidth in PHP userland with e.g. bandwidth-throttle/bandwidth-throttle

use bandwidthThrottle\BandwidthThrottle;

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

$throttle = new BandwidthThrottle();

if ($user->isPremium()) {
    $throttle->setRate(500, BandwidthThrottle::KIBIBYTES); // 500KiB/s
} else {
    $throttle->setRate(50, BandwidthThrottle::KIBIBYTES); // 50KiB/s
}

$throttle->throttle($out);

stream_copy_to_stream($in, $out);

Upvotes: 0

Mike D.
Mike D.

Reputation: 4104

This would be something implemented at the web-server level. This question will probably cover how to implement the throttling if you're using apache: How can I implement rate limiting with Apache? (requests per second)

As for doing it on a per-user basis there may be a way to interface with these apache configuration directives from php or you could just have two virtual hosts with one being locked down to certain users and with a higher throttle rate.

Upvotes: 1

Related Questions