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.

 

$your_file = ‘file.zip’;
$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!’);
}

About the author

PHP | MVC | Codeignitor | Zend | Yii | Smarty | Android | Laravel Expert Senior Software Developer.I love my job and feel happy to working on new ideas and technologies.

COMMENTS (14)

Admiring the time and energy you put into your blog and detailed information you offer.
It’s nice to come across a blog every once in
a while that isn’t the same outdated rehashed information.
Wonderful read! I’ve saved your site and I’m including your RSS feeds
to my Google account.

Oh my goodness! Incredible article dude! Many thanks, However I am having difficulties with your
RSS. I don’t understand the reason why I can’t subscribe to it.
Is there anyone else getting identical RSS issues? Anyone who knows the solution can you kindly respond?
Thanx!!

    Yes, just checking the RSS feed problem and replay you soon. Hopefully it will work.. :)

    @google adsense rss issue has been solved now please check it..you fill find rss feeds link on the side bar.. :)

What’s up, after reading this remarkable article i am
as well glad to share my know-how here with mates.

Reply

xD250B Whoa! This blog looks exactly like my old one! It’s on a entirely different subject but it has pretty much the same page layout and design. Wonderful choice of colors!

Reply

UvfvXt It’s in reality a great and helpful piece of information. I’m satisfied that you shared this useful information with us. Please keep us informed like this. Thank you for sharing.

The time to read or check out the subject material or sites we’ve linked to below.

The time to read or check out the content material or web-sites we have linked to below.

Reply

X1LfZe It is in point of fact a great and helpful piece of information. I’m glad that you simply shared this useful info with us. Please keep us up to date like this. Thank you for sharing.

I simply want to mention I’m newbie to blogging and site-building and absolutely enjoyed your web site. Most likely I’m likely to bookmark your blog post . You actually come with impressive articles and reviews. Thanks for sharing your website page.

I just want to tell you that I am newbie to blogs and seriously liked this web site. Likely I’m want to bookmark your blog post . You certainly come with awesome well written articles. Thanks a bunch for revealing your website page.

Reply

PB5jWt Thanks a lot for the blog article. Keep writing.

Reply

d6N0V7 Im obliged for the article post.Much thanks again. Fantastic.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Name (required)
Email (required)