Important alert: (current site time 5/4/2015 6:37:58 AM EDT)


Bandwidth control with PHP

Submitted on: 3/3/2007 11:31:29 AM
By: sabeen malik  
Level: Intermediate
User Rating: By 4 Users
Compatibility: PHP 4.0
Views: 18335
(About the author)
     Its a pointer to someone looking for a bandwidth control mechanism via pure PHP implementation

Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

I was given a task to restrict number of downloads , divide the total bandwidth into 2 groups and control the speed of the downloads. So i came up with the idea of doing it with PHP cause mod_bandwidth or mod_throttle werent tailored for the job. Here is the part where i attempt to control the speed of the download.

= "files/abc.exe";
$time = 10000;
$obytes = 150*1024; //150k download speed restriction
$fd = fopen ($filedownload, "rb");
while (!
feof ($fd)) {
$usec, $sec) = explode(" ", microtime());    
$time_start = $usec + $sec;
$bytes = ceil($obytes/100);
fread($fd, $bytes);
$time < 10000) usleep(10000-$time);
$usec, $sec) = explode(" ", microtime());
$time_end = $usec + $sec;
$time =ceil(($time_end - $time_start)*1000000)+10;
fclose ($fd);

Basically we know how many max bytes we need to send per sec , so we calculate the time in microseconds it took for the code iteration to take place and deduct that time from 1 sec and usleep it for that long , if it took more than 1 sec to complete the iteration ( for example the user is on dialup and cannt receive 150k per sec) than dont put in the delay.

Hope this helps someone. You can find more PHP tips on my site but this one had to be shared in a better way :)

Other 3 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

3/7/2007 9:22:18 AMv.smarties

$filedownload = "files/abc.exe";

what is this?????
where is file abc.exe
pls reply
(If this comment was disrespectful, please report it.)

3/7/2007 1:47:28 PMSjoerd


I'm not really sure whether you have got a lot of PHP knowledge but I truly doubt so.
$filedownload = "files/abc.exe"; , is an example file which can be changed to whatever file you want to control the speed of.
@ Author, you'll get 3 globes from me.
It's a good solution, but it will not work if the file is send to slow because a PHP script has got a timeout (most systems have got it set at 30 secs I thought).
So if the uploading would take over 30 seconds, the script will just abort and so will the download.
(If this comment was disrespectful, please report it.)

3/7/2007 1:58:46 PMsabeen malik

as i said , this is just a pointer , a tip to someone looking at a solution , i am successfully using this core idea in downloading files upto 600mb , with resume support. I have set my default time limit on the server to 0 , so if it is causing trouble for you , you can put this on the top of the script.


as for "where is file abc.exe" , its just an example filename of the file you want to control the download speed of.That needs to be on the server, its just an example filename. You should change it according to your situation.
(If this comment was disrespectful, please report it.)

3/7/2007 4:22:15 PMSjoerd

@ Author
That's true, my apologies. It deserves 5 globes at this level. You got them ;)
(If this comment was disrespectful, please report it.)

3/7/2007 5:54:24 PMsabeen malik

thank you :)
(If this comment was disrespectful, please report it.)

3/19/2007 2:58:10 PMBethrezen

lol. man. it's really 5tupid thing. Apache has it's own bandwidth control. So if you do `sleep` you make a lot of CPU%.
Another thing is that this thing can't allow user to download file with any download manager.... So its poor!
Even bad for newbies!
(If this comment was disrespectful, please report it.)

3/19/2007 3:50:55 PMsabeen malik

Thanks for your response , i appreciate constructive criticism.

As i said its only a pointer to someone looking at the idea of doing it with PHP , its not for commercial apps which would totally depend on people download 100s or 1000s of files at a time.As i responded to another user , i have implemented the code to resume downloads as well , so thats a problem with a bit of effort that can be done too.

As for sleep putting load on CPU , the usleep function is putting it to sleep less than a sec and i have tested it with several concurrent downloads. seems to do the job if you cannt get the bandwidth control modules to work. As the bandwidth control is not a native apache function you have to get the module configured for that which alot of php users with shared hosting cannt do.

Also please understand the mod_bandwidth also uses sleep :)

Hope this helps you understand the target audience for this.
(If this comment was disrespectful, please report it.)

10/24/2007 10:00:59 AMVince Hahn

You know, I've never thought of a use for the sleep or usleep functions until I looked at this code. Seriously other than what you've done here I'm (again) having trouble finding a use for them. That aside, I'd like to express how amazing this code is. It's simple, easy to implement for personal/semi-organizational usage, and small. I only needed something that can handle 50MB or so. Using your example I tried making a simpler one based on sleep() but the download speed is considerably slower than what I'm trying to allow. Like for instance I have it set to 100*1024 (100kbps obviously) but I'm getting only just over half that. I'm just wondering if you could explain why. I reverted to your method and I seem to be having no trouble. 5 Globes from me. I only wish I could give your code a higher score.
(If this comment was disrespectful, please report it.)


Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.