Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-27-2009, 11:59 PM   PM User | #1
IFeelYourPain
Regular Coder

 
Join Date: Sep 2007
Posts: 250
Thanks: 19
Thanked 0 Times in 0 Posts
IFeelYourPain has a little shameless behaviour in the past
Limit Download Speed

Is there a way to limit download speed via a single page?

i.e. My download page is download.php and my files are stored in /attachments which is one directory above the file?

I want to use php however though because I need to run conditionals which exclude certain members of my website. Maybe htaccess in the directory and php code in the download page?
IFeelYourPain is offline   Reply With Quote
Old 10-28-2009, 09:43 PM   PM User | #2
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
Well let's see...

I googled, found this site:
http://bytes.com/topic/php/answers/3...download-speed

and then found this code:
PHP Code:
<?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; 

?>
Have you tried any of these steps yet?
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 10-29-2009, 04:29 AM   PM User | #3
IFeelYourPain
Regular Coder

 
Join Date: Sep 2007
Posts: 250
Thanks: 19
Thanked 0 Times in 0 Posts
IFeelYourPain has a little shameless behaviour in the past
Quote:
Originally Posted by Rowsdower! View Post
Well let's see...

I googled, found this site:
http://bytes.com/topic/php/answers/3...download-speed

and then found this code:
PHP Code:
<?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; 

?>
Have you tried any of these steps yet?
Nope haven't seen this?

What data is $file pulling from? Cause most of them you have a preset file like $file = 'file.rar';

I just want it so my attachments.php will make all downloads on that page be like 200 kbps.
IFeelYourPain is offline   Reply With Quote
Old 10-29-2009, 12:38 PM   PM User | #4
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
You could set $file based on a session variable or a $_GET (written into the URL - good for direct linking or bookmarking) or $_POST (passed invisibly through the backend - not so good for direct linking or bookmarking) variable without any trouble at all. That would keep things dynamic enough to work for any file you are offering for download.
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 11-12-2009, 06:48 PM   PM User | #5
IFeelYourPain
Regular Coder

 
Join Date: Sep 2007
Posts: 250
Thanks: 19
Thanked 0 Times in 0 Posts
IFeelYourPain has a little shameless behaviour in the past
I'm confused.
IFeelYourPain is offline   Reply With Quote
Old 11-12-2009, 06:55 PM   PM User | #6
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
Which part are you confused about? Do you know what session variables and $_GET variables are?
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 11-13-2009, 05:33 AM   PM User | #7
IFeelYourPain
Regular Coder

 
Join Date: Sep 2007
Posts: 250
Thanks: 19
Thanked 0 Times in 0 Posts
IFeelYourPain has a little shameless behaviour in the past
Quote:
Originally Posted by Rowsdower! View Post
Which part are you confused about? Do you know what session variables and $_GET variables are?
Yea they are used n form submissions right?
IFeelYourPain is offline   Reply With Quote
Old 11-14-2009, 10:43 PM   PM User | #8
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
They can be used in forms but they don't HAVE to be. In fact $_POST variables are more common for forms than $_GET variables are.

You should take some time to actually read about sessions, session variables, and $_GET variables (and $_POST variables while you're at it).

If you are just waiting for me to spoon-feed you the answer then you are in for a loooong wait.
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:19 PM.


Advertisement
Log in to turn off these ads.