• Domů
  • Kontakt
  • O mně
  • Reference
  • Služby
Blue Orange Green Pink Purple

PHP: omezení rychlosti stahování souboru s podporou download manageru

Posted in php. on Neděle, Leden 17th, 2010 by admin Tags: php
Led 17
?Ukaž kód PHP
<?php
/*
   send_file( string $file [, int $rate ] )
 
   param $file - Path to the file to send
   param $rate - Speed limit of download in kB/s
*/
function send_file($file, $rate = 0) {
 
   // check if the file exists
   if (!is_file($file))
   {
      die('404 File Not Found');
   }
 
   // get the filename, extension, and size
   $filename = basename($file);
   $file_extension = strtolower(substr(strrchr($filename, '.'), 1));
   $size = filesize($file); 
 
   // set the mime type based on the extension
   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;
      //  block access to sensitive file types
      case 'php':
      case 'inc':
         exit;
      break;
      default:
         $ctype='application/force-download';
   }
 
   // begin writing headers
   header('Cache-Control: private');
   header('Content-Type: ' . $ctype);
   header('Content-Disposition: attachment; filename=' . $filename);
   header('Content-Transfer-Encoding: binary');
   header('Accept-Ranges: bytes');
 
   // open the file for reading
   $fp = fopen($file, 'rb');
 
   // check if http_range was sent by client
   if(isset($_SERVER['HTTP_RANGE']))
   {
      // if so, calculate the range to use
      $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]);
      }
 
      // seek to the requested position in the file
      fseek($fp, $seek_start);
 
      // set the range response headers
      header('HTTP/1.1 206 Partial Content');
      header('Content-Length: ' . ($seek_end - $seek_start + 1));
      header(sprintf('Content-Range: bytes %d-%d/%d', $seek_start, $seek_end, $size));
   }
   else
   {
      // set default response headers
      header('Content-Length: ' . $size);
   }
 
   // set up the size of each piece of data we send
   $block_size = 1024;
   if($rate > 0)
   {
      // multiply by rate if specified
      $block_size *= $rate;
   }
 
   // prevent the script from timing out
   set_time_limit(0);
 
   // start sending the file
   while(!feof($fp))
   {
      // output data
      print(fread($fp, $block_size));
      flush();
 
      if($rate > 0)
      {
         // wait one second before next block if rate is specified
         sleep(1);
      }
   }
 
   // close the file
   fclose($fp);
}
?>

7 Comments

  1. sts on Květen 31st, 2010

    You post awsome posts. Bookmarked !

  2. Fabian Huxley on Duben 14th, 2011

    I am not able to view this website correctly on opera I think there’s a downside

  3. Choulnard62 on Duben 15th, 2011

    Good day, Fantastic site, exactly where did you come up of the facts in this summation? Im glad I observed it though, I will be checking back soon to determine what other posts you have.

  4. Szwede on Duben 18th, 2011

    Hi there, I loved studying your post. Thanks for the great info. Hoped that we can prolong our friendship by a mutual link exchange? Let me know, and nice to see you here!

  5. Cornell Alpheaus on Duben 19th, 2011

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is magnificent blog. A fantastic read. I will certainly be back.

  6. Online Hotel Booking on Duben 19th, 2011

    I was suggested this blog by means of my bro. I am not sure whether or not this publish is written by means of him as no one else understand such precise about my problem. You’re wonderful! Thanks!

  7. download limitless the movie on Duben 19th, 2011

    Usually I do not post on blogs, but I would like to say that this post extremely forced me to complete so! Thanks, really nice article.



fandaa.name

    ¨
  • Hledání na webu


  • O mně
    Jmenuji se František ‚fandaa‘ Polášek. Je mi 18 let a mezi mé záliby patří programování (popř. skriptování a kódování) v jazycích (X)HTML, CSS, PHP, SQL, Javascript (+ AJAX), C/C++ a C#.
  • Rubriky
    • php (9)
  • Odkazy
    • Hry zdarma
    • Lukyn.com
    • Online-Firmy.com
    • Povinné ručení levně
    • SvětAut.com
    • Terorista.cz
  • Archives
    • Leden 2010




  • Home
  • Kontakt
  • O mně
  • Reference
  • Služby

© Copyright fandaa.name. All rights reserved.
Designed by FTL Free WordPress Themes brought to you by [i] Website Templates

Back to Top