问题描述:

I have a situation where I need to be able to throttle / limit the upload speed (outgoing server connection) for each of my users using PHP. The reason why is because I'm running a small streaming website for my family and friends and currently each user can download data from the server at the users highest possible speed which causes other users to suffer due to lack of bandwidth on my server.

I did some calculations and I noticed that each user only need 1 Mbps to be able to stream movies from my server without any buffering.

So my question is:

How can I throttle / limit the servers upload speed per user using PHP?

I would be very happy if you could provide an example of how I can accomplish this, as of now I have no idea how i can do this.

网友答案:

You could use bandwidth-throttle/bandwidth-throttle

use bandwidthThrottle\BandwidthThrottle;

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

$throttle = new BandwidthThrottle();
$throttle->setRate(1, BandwidthThrottle::MIBIBYTES); // Set limit to 1MiB/s
$throttle->throttle($out);

stream_copy_to_stream($in, $out);
相关阅读:
Top