I'm going to implement a website in PHP using a Web Framework. I never implemented a website in PHP... I don't have problems learning new languages. The problem is that I would like to know if frameworks like Zend, CakePHP can be used to create a page which lets you download files at a given rate (eg 50 KB/s)? Thank you.
5 Answers
Your server should deal with this issue, not PHP.
In case you have Apache see here:
For Lighttpd see here:

- 14,365
- 5
- 52
- 77
-
If you serve files that require a check if the user has some kind of user level or permission and you want to change the limit based on that, how is this going to work with just a sever side solution? – floriank Apr 25 '12 at 10:39
-
1my guess: serving through different locations: `
` and ` – mark Apr 25 '12 at 10:42` -
Yeah I think so as mark, you can handle your permissions in this route then. With the `Selective traffic shaping plugin` in the TrafficShaping link for Lighttpd there's an much easier way with just sending headers. – dan-lee Apr 25 '12 at 10:48
-
But as fair as I know most Servers still come with Apache2.2 - mod_ratelimit seems to be 2.3 and up. – mark Apr 29 '12 at 11:48
As far as I know limiting the download speed is not part of the core but that is very easy to implement, simply extend the MediaView class and add that simple feature to it.

- 25,546
- 9
- 42
- 66
-
-
-
Because this is a problem that should be handled by the webserver. Why use PHP to fake the "rate limiting" and use up extra resources, when the webserver (with a bit of modification/plugins) can do so with much more efficiency? In addition, if the asker is using Apache, then a new thread is created for each PHP request. By using `sleep()` to artificially slow script execution, the resource being held by the thread is effectively locked, and the total amount of clients we can serve would decrease dramatically. – F21 Apr 25 '12 at 12:20
PHP is not very good language for limiting download in my opinion. I have never done it, but I would do it in this way
header('Content-type: image/jpeg');
header('Content-Disposition: attachment; filename="image.jpg"');
$f = file('may_image_or_file_to_download.jpg');
foreach($f as $line){
echo $line;
flush();
usleep(10000); //change sleep time to adjusting download speed
}
you better use some Apache mods if you have possibilities
sorry for my english

- 735
- 8
- 16
All you can do with PHP is possible in these Frameworks, too. The trick is to do it without violating the rules of the framework (e.g. the MVC pattern).
In CakePHP it is absolutely possible to create a controller action which puts out a binary file with all needed headers. In your controller action you can than limit the download speed with standard php.

- 1,276
- 2
- 12
- 17
If the framework doesn't provide that feature use an existing library, e.g. bandwidth-throttle/bandwidth-throttle
use bandwidthThrottle\BandwidthThrottle;
$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->throttle($out);
stream_copy_to_stream($in, $out);

- 7,738
- 2
- 38
- 67