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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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.
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 :)
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.)
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.)
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.)
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.)
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.)