Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I found this thread, but am hesitant when it comes to screwing with my network settings.

What I want to do is limit the rate that people can download files from my webserver remotely. LAN connections should still be able to transfer at full speed, but anything outside of the local network should be limited to 200Kbps per connection. Is that even possible? Can I limit the number of connections per client (probably by IP)?

If this can't be done at the OS level, is there any way I could script it using PHP? Maybe read X bytes into a file and then sleep...

Thanks in advance!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I'm sure something like this is achievable since there are file storage/sharing sites that limit download speeds as well as simultaneous downloads. I personally do not know how to implement this type of thing, but I can hopefully point you in the right direction.

Take a look at the Token Bucket and Leaky Bucket algorithms.

I believe there are apache modules you can install and configure to help with this sort of thing. Try looking into mod_cband and iptables.

Good luck!

UPDATE

If you're looking for a way to do it via PHP, you could try this (source):

// local file that should be send to the client
$local_file = 'test-file.zip';

// filename that the user gets as default
$download_file = 'your-download-name.zip';

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;

if(file_exists($local_file) && is_file($local_file)) {

    // send headers
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);

    // flush content
    flush();

    // open file stream
    $file = fopen($local_file, "r");

    while (!feof($file)) {

        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));

        // flush the content to the browser
        flush();

        // sleep one second
        sleep(1);
    }

    // close file stream
    fclose($file);

}
else {
    die('Error: The file '.$local_file.' does not exist!');
}
share|improve this answer
    
Looks like cband is gone. :( –  GameCharmer Jan 17 '13 at 23:08
    
ah, that's a bummer... any luck with iptables? at least you know you have a fallback with PHP, but certainly not ideal, especially given your need to handle LAN connections differently. –  mason81 Jan 18 '13 at 14:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.