Connecting Tech Pros Worldwide Help | Site Map

Using PHP to limit download speed

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 19th, 2005, 05:26 PM
Newbie
 
Join Date: Oct 2005
Age: 30
Posts: 9
Default Using PHP to limit download speed

:confused::confused:

I have been on the quest to find a php script that can serve files for downloads and limit the speed at which the file is transfered to the user. I want a faster download speed for registered members and a slower speed for guests.

The only other thing I can think of is using the header() function to force a download for the requested file type and stream the file to the user's browser. Then using sleep() or usleep() on a loop until the download completes.

Anyone have any suggestions or ideas?

~Chipgraphics
Reply
  #2  
Old November 20th, 2005, 12:59 PM
KUB365's Avatar
Administrator
 
Join Date: Jul 2005
Location: Portland, OR
Age: 28
Posts: 861
Default

Here is a snippet i found on php.net.

Expand|Select|Wrap|Line Numbers
  1. [font=Courier New]<?php
  2.  
  3. $file = "test.mp3"; [/font][font=Courier New]// file to be send to the client
  4. $speed = 8.5; [/font][font=Courier New]// 8,5 kb/s download rate limit
  5.  
  6. if(file_exists($file) && is_file($file[/font][font=Courier New])) {
  7.  
  8.    header("Cache-control: private"[/font][font=Courier New]);
  9.    header("Content-Type: application/octet-stream"[/font][font=Courier New]); 
  10.    header("Content-Length: ".filesize($file[/font][font=Courier New]));
  11.    header("Content-Disposition: filename=$file" . "%20"[/font][font=Courier New]); 
  12.  
  13.    flush[/font][font=Courier New]();
  14.  
  15.    $fd = fopen($file, "r"[/font][font=Courier New]);
  16.    while(!feof($fd[/font][font=Courier New])) {
  17.          echo fread($fd, round($speed*1024[/font][font=Courier New]));
  18.        flush[/font][font=Courier New]();
  19.        sleep(1[/font][font=Courier New]);
  20.    }
  21.    fclose ($fd[/font][font=Courier New]);
  22.  
  23. }
  24. ?> [/font]
  25.  
Link to page and exact code.
Reply
  #3  
Old November 29th, 2005, 08:52 PM
Newbie
 
Join Date: Oct 2005
Age: 30
Posts: 9
Default I combined a couple together and tweaked them out and it works!

With the constant change of protocols and programming yada yada. I read over what you had suggested and combined a couple of the concepts into one package deal. And it worked on the first try ! :D So, thank you for pointing me in the right direction ! I'll list my source code below.

Here's a function to use PHP to control download bandwith speed:
All original code I borrowed from PHP's online documentation found HERE.

[PHP]
<?php
function send_file($file, $speed = 100) {

//First, see if the file exists
if (!is_file($file)) {
die("<b>404 File not found!</b>");
}
//Gather relevent info about file
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
// This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
case "exe":
$ctype="application/octet-stream";
break;
case "zip":
$ctype="application/zip";
break;
case "mp3":
$ctype="audio/mpeg";
break;
case "mpg":
$ctype="video/mpeg";
break;
case "avi":
$ctype="video/x-msvideo";
break;

// The following are for extensions that shouldn't be downloaded
// (sensitive stuff, like php files)
case "php":
case "htm":
case "html":
case "txt":
die("<b>Cannot be used for ". $file_extension ." files!</b>");
break;
default:
$ctype="application/force-download";
}

// Begin writing headers
header("Cache-Control:");
header("Cache-Control: public");
header("Content-Type: $ctype");

$filespaces = str_replace("_", " ", $filename);
// if your filename contains underscores, replace them with spaces

$header='Content-Disposition: attachment; filename='.$filespaces;
header($header);
header("Accept-Ranges: bytes");

$size = filesize($file);
// check if http_range is sent by browser (or download manager)
if(isset($_SERVER['HTTP_RANGE'])) {
// if yes, download missing part

$seek_range = substr($_SERVER['HTTP_RANGE'] , 6);
$range = explode( '-', $seek_range);
if($range[0] > 0) { $seek_start = intval($range[0]); }
if($range[1] > 0) { $seek_end = intval($range[1]); }

header("HTTP/1.1 206 Partial Content");
header("Content-Length: " . ($seek_end - $seek_start + 1));
header("Content-Range: bytes $seek_start-$seek_end/$size");
} else {
header("Content-Range: bytes 0-$seek_end/$size");
header("Content-Length: $size");
}
//open the file
$fp = fopen("$file","rb");

//seek to start of missing part
fseek($fp,$seek_start);

//start buffered download
while(!feof($fp)) {
//reset time limit for big files
set_time_limit(0);
print(fread($fp,1024*$speed));
flush();
sleep(1);
}
fclose($fp);
exit;
}
?>[/PHP]

I hope that helps someone out too. Thanks again for pointing me in the right direction !!


~Chipgraphics
Reply
  #4  
Old November 29th, 2005, 09:14 PM
KUB365's Avatar
Administrator
 
Join Date: Jul 2005
Location: Portland, OR
Age: 28
Posts: 861
Default

Hey, congrats on getting the results you were looking for.

Thanks for posting the code for people to learn from, that's what this forum is all about.


~KUB
Reply
  #5  
Old March 8th, 2006, 02:36 PM
Newbie
 
Join Date: Mar 2006
Posts: 2
Default

I used that script from php.net but i have a problem. When i click on download it starts download, but on ~600kB it stops and dont download....
can abybody explain me thit??
Reply
  #6  
Old March 8th, 2006, 04:06 PM
KUB365's Avatar
Administrator
 
Join Date: Jul 2005
Location: Portland, OR
Age: 28
Posts: 861
Default

Download issues from their server. Maybe wait for a few hrs and try again.
Reply
  #7  
Old March 8th, 2006, 04:19 PM
Newbie
 
Join Date: Mar 2006
Posts: 2
Default

Quote:
Originally Posted by KUB365
Download issues from their server. Maybe wait for a few hrs and try again.
it is localhost.....
can it be some option of apache ???
in my webhosting i tried to download 20 MB and it was OK....
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.