Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2007
    Location
    California, United States
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,048
    Thanks
    10
    Thanked 93 Times in 91 Posts
    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)

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,048
    Thanks
    10
    Thanked 93 Times in 91 Posts
    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)

  • #5
    New Coder
    Join Date
    Jan 2007
    Location
    California, United States
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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? )

  • #6
    New Coder
    Join Date
    Dec 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.)

  • #7
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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();"

  • #8
    New Coder
    Join Date
    Jan 2007
    Location
    California, United States
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #9
    New Coder
    Join Date
    Aug 2007
    Location
    Seattle, WA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •