2

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
user1934039
  • 23
  • 1
  • 4
  • Edit: I think I found something here: http://stackoverflow.com/questions/4002106/limit-download-speed-using-php at least it seems to work (currently testing it with consistent 75 kb/s). – user1934039 Apr 08 '13 at 07:52
  • That is very cool, I like it. – Mike D. Apr 08 '13 at 07:54
  • In regards to the link I posted above, it is the bottom script on that page I am testing: http://stackoverflow.com/a/12245044/1934039 – user1934039 Apr 08 '13 at 08:01

2 Answers2

1

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.

Community
  • 1
  • 1
Mike D.
  • 4,034
  • 2
  • 26
  • 41
0

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);
Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67