Limit File Download Speed (Using PHP)

Yes, using simple PHP snippet you can limit the download speed of a file. This can be done for Bandwidth throttling or for Rate limiting. One practical application of this script will be slowing down the speed at which someone can download a file for free. If you pay for the service then the speed would be either unlimited or less restrictive.
Another application would be if you are generating the file as it is being created. Like, If there is a 10mb lossless image, and you know that it takes at most 1 second to create 256kb of it, then you can set the script to stream 256kb every second. This way the user can start receiving data as soon as the first 256kb are ready.
{code type=php}
// The file that will be sent to the user
$your_file = ‘file.zip’;

// Rename the file name
$new_file = ‘new-filename.zip’;

// Set the download speed limit (70 kb/s)
$download_speed = 70;

if(file_exists($myl_file) && is_file($my_file)) {

// Headers
header(‘Cache-control: private’);
header(‘Content-Type: application/octet-stream’);
header(‘Content-Length: ‘.filesize($my_file));
header(‘Content-Disposition: filename=’.$new_file);

// Flush the content
flush();

// File stream
$file = fopen($my_file, “r”);

while (!feof($file)) {

// Send the current part of the file to the browser
echo fread($file, round($download_speed* 1024));

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

// Sleep one second
sleep(1);
}

// Close file stream
fclose($file);

}
else {
die(‘Error: The file ‘.$my_file.’ does not exist!’);
}
{/code}
This script can be used to limit the download speed at which one can download the file. You can also try out QoS Bandwidth Throttle in PHP.

  • Thanks for the nice post. This may help me for my site http://downloadnp.com .

    And one more thing i want to know: If i get lots of downloaders in my site at once, does it affect the loading speed of site pages for other visitors or not?