Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – Speed Limit Download of File

Speed Limit Download of File

A simple and effective way to limit the rate at which a file is downloaded. Good for user level file transfers. If you still want to allow guests to download files at a slower speed this might be the answer.

Snippet:

  1. <?
  2.  // speed is in kB/s
  3.  // $dst_filename is what the user gets
  4.  // $src_filename is the file on the server
  5.  // http://thedevnet.com/php/snippets/speed-limit-download-of-file/
  6.  
  7.  function dl_speedcap($src_filename, $dst_filename, $speed)
  8.  {
  9.   // local file
  10.   $local_file = $src_filename;
  11.   // filename that the user gets
  12.   $download_file = $dst_filename;
  13.    
  14.   // set the speed
  15.   $download_rate = $speed;
  16.   if(file_exists($local_file) && is_file($local_file))
  17.   {
  18.    // send headers
  19.    header(&#039;Cache-control: private&#039;);
  20.    header(&#039;Content-Type: application/octet-stream&#039;);
  21.    header(&#039;Content-Length: &#039;.filesize($local_file));
  22.    header(&#039;Content-Disposition: filename=&#039;.$download_file);
  23.    
  24.    // flush content
  25.    flush();    
  26.    // open file stream
  27.    $file = fopen($local_file, "r");    
  28.    while(!feof($file)) {
  29.    
  30.     // send the current file part to the browser
  31.     print fread($file, round($download_rate * 1024));    
  32.    
  33.     // flush the content to the browser
  34.     flush();
  35.    
  36.     // sleep one second
  37.     sleep(1);    
  38.    }    
  39.    
  40.    // close file stream
  41.    fclose($file);
  42.   }
  43.   else
  44.   {
  45.    die(&#039;Error: The file &#039;.$local_file.&#039; does not exist!&#039;);
  46.   }
  47.  }
  48. ?>

Example:

  1. <?
  2.  dl_speedcap("myfile.zip", "theirfile.zip", "8");
  3. ?>
GD Star Rating
loading...

Originally posted 2009-10-17 02:55:08.

Popularity: 12%

Posted by irbobo   @   8 August 2010

Related Posts

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

1 Comments

Comments
Oct 17, 2009
7:58 am
#1 irbobo :

perhaps a modification that would allow slower media downloading might be possible with this snippet

GD Star Rating
loading...
Leave a Comment

You must be logged in to post a comment.

Previous Post
« Get Data Between Two Elements
Next Post
Twitter Class »
Powered by Wordpress   |   Lunated designed by ZenVerse