Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

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 01-10-2007, 02:04 PM   PM User | #1
frosty1433
New Coder

 
Join Date: Jan 2007
Location: California, United States
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
frosty1433 is an unknown quantity at this point
Function to force download at certain speed

PHP Code:
function force_download($file$filename=""$directory="/"$downloadspeed="10")
{
 if (
$directory != "" || substr($directory, -11) != "/")
 {
  
$directory $directory "/";
 }
 if (
$filename == "")
 {
  
$filename $file;
 }
 
$filepath $directory $file;
 unset(
$file$directory);
 if (!
$file fopen($filepath'r'))
 {
  return 
false;
 }
 else
 {
  
header("Cache-control: private");
  
header('Content-Description: File Transfer'); 
  
header('Content-Type: application/force-download'); 
  
header('Content-Length: ' filesize($filepath)); 
  
header('Content-Disposition: attachment; filename=' $filename);
  while (!
feof($file))
  {
   echo 
fread($file$downloadspeed 1024 8);
   
flush();
   
sleep(1);
  }
  @
fclose($file);
 }
 return 
true;

Credit:
http://www.phpnerds.com/article/spee...file-downloads
frosty1433 is offline   Reply With Quote
Old 01-11-2007, 02:37 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
why would you want to slow down the download ... and your server ? if there is a reason (and there may well be)... what is it ?
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 01-11-2007, 06:49 PM   PM User | #3
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
For example if your server doesn't have all the upload speed you want you can allow some bandwidth for others. If your site depends on it, you could let paying users download faster.
You asked for reasons, not if it's viable in PHP.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 01-12-2007, 12:10 AM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Quote:
Originally Posted by marek_mar View Post
... you could let paying users download faster.
fair enough.. never thought of that one.
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 01-19-2007, 07:18 AM   PM User | #5
frosty1433
New Coder

 
Join Date: Jan 2007
Location: California, United States
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
frosty1433 is an unknown quantity at this point
Well, I've discovered that this script is downloading slightly faster than it should be, but not by much. (Maybe my internet is just too fast? )
frosty1433 is offline   Reply With Quote
Old 01-21-2007, 01:09 PM   PM User | #6
mattd8752
New Coder

 
Join Date: Dec 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
mattd8752 is an unknown quantity at this point
If your looking for another reason, you could make it so opening images from on other servers is very slow. (And forces them to be taken as apps, forcing sites to download your images and host them on their site, not hotlink, by checking if the image is being called by your server.)
mattd8752 is offline   Reply With Quote
Old 01-23-2007, 07:30 PM   PM User | #7
Velox Letum
Senior Coder

 
Join Date: Apr 2005
Location: Colorado, United States
Posts: 1,208
Thanks: 0
Thanked 0 Times in 0 Posts
Velox Letum is an unknown quantity at this point
Quote:
Originally Posted by mattd8752 View Post
If your looking for another reason, you could make it so opening images from on other servers is very slow. (And forces them to be taken as apps, forcing sites to download your images and host them on their site, not hotlink, by checking if the image is being called by your server.)
That's a rather contrived use. If you want to block hotlinking, there's much easier ways to do so, such as rewrite rules.

Code:
RewriteEngine    on
RewriteCond    %{HTTP_REFERER} !^$
RewriteCond    %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?yoursite\.com [NC]
RewriteRule    \.(gif|jpe?g|png)$ - [F,NC,L]
__________________
"$question = ( to() ) ? be() : ~be();"
Velox Letum is offline   Reply With Quote
Old 01-23-2007, 11:45 PM   PM User | #8
frosty1433
New Coder

 
Join Date: Jan 2007
Location: California, United States
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
frosty1433 is an unknown quantity at this point
An idea I had for this is to right a code to moniter how many users are on your server, and divide the download speed evenly to everyone, to prevent the server from overloading. That was my original idea with this... (I have no idea if that would work though, but it sounds nice :P)
frosty1433 is offline   Reply With Quote
Old 08-11-2007, 11:13 PM   PM User | #9
thiscolddecembe
New Coder

 
Join Date: Aug 2007
Location: Seattle, WA
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
thiscolddecembe is an unknown quantity at this point
Code:
...$online_users = mysql_num_rows($result);
//total bandwidth you want to use (downloading)
$totalb = 2000;
function force_download($file, $filename="", $directory="/", $downloadspeed="$totalb")
Something like that? Not sure about specifics.
thiscolddecembe 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 07:21 AM.


Advertisement
Log in to turn off these ads.