Led 17
PHP: omezení rychlosti stahování souboru s podporou download manageru
<?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); } ?> |
