Wednesday, February 16, 2011

Control Download Speed

Hi,

If you are creating a media repository and want to control the download speed of files for different users, then that can be easily achieved by PHP. The basic idea is when you are downloading a MP3 or any other format file, make the download via a .php file (Force Download). Now in force download you change the page header with respect to the file extension.

For downloading in force download you need to the read the content of the file and print it. So now we have the header and the echo or print command for displaying the content of the file. This will trigger the download functionality for any file.

The trick of controlling the download speed is, when you are reading and printing the file content don't read the entire file at a time, but read it "x" bytes at a time in a file reading loop. This "x" is the kbps speed you are downloading the file.

To make it simple here is the code:


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

     if (!is_file($file)) {
         die("<b>404 File not found!</b>");
     } 
     $filename = basename($file);
     $file_extension = strtolower(substr(strrchr($filename,"."),1));
     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;

         case "php":
         case "htm":
         case "html":
         case "txt":
             die("<b>Cannot be used for ". $file_extension ." files!</b>");
             break;
         default:
             $ctype="application/force-download";
     }
 
     header("Cache-Control:");
     header("Cache-Control: public");
     header("Content-Type: $ctype");
 
     $filespaces = str_replace("_", " ", $filename);

     $header='Content-Disposition: attachment; filename='.$filespaces;
     header($header);
     header("Accept-Ranges: bytes");
 
     $size = filesize($file); 
     if(isset($_SERVER['HTTP_RANGE'])) {
         $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");
     } 
     $fp = fopen("$file","rb");

     fseek($fp,$seek_start);
 
     while(!feof($fp)) {     
        set_time_limit(0);     
         print(fread($fp,1024*$speed));
         flush();
         sleep(1);
     }
     fclose($fp);
     exit;
 }

send_file("song.mp3", 10); // download speed 10kb/s

?>

Best of luck. Don't forget to post your comments ;)

No comments:

Post a Comment