$first=0, $last=499 . $range='500-', $filesize=1000 => $first=500, $last=999 . $range='500-1200', $filesize=1000 => $first=500, $last=999 . $range='-200', $filesize=1000 => $first=800, $last=999 . */ $dash=strpos($range,'-'); $first=trim(substr($range,0,$dash)); $last=trim(substr($range,$dash+1)); if ($first=='') { //suffix byte range: gets last n bytes $suffix=$last; $last=$filesize-1; $first=$filesize-$suffix; if($first<0) $first=0; } else { if ($last=='' || $last>$filesize-1) $last=$filesize-1; } if($first>$last){ //unsatisfiable range header("Status: 416 Requested range not satisfiable"); header("Content-Range: */$filesize"); exit; } } // Sends the specified byte range from the file. function send_file($filename, $start, $end) { flush(); // make sure headers are sent asap. $bytes_sent = 0; $bytes_remaining = $end - $start + 1; $rate = isset($_REQUEST['rate']) ? $_REQUEST['rate'] : 9999999; $do_rate2 = isset($_REQUEST['rate2']); $rate2 = $_REQUEST['rate2']; $rate2_at = $_REQUEST['rate2at'] - $start; // no default $do_stall = isset($_REQUEST['stallat']); $stall_at = $_REQUEST['stallat'] - $start; // no default $stall_time = isset($_REQUEST['stallfor']) ? $_REQUEST['stallfor'] : 7; // Open the file, and seek to our start point $file = fopen($filename, "rb"); fseek($file, $start); // loop over data, sending a burst every 100ms. while (!feof($file) && $bytes_remaining) { $burst_size = round($rate * 1024 / 8 / 10); // kbps to Bps, 10x per second // Can't send more than we have! if ($burst_size > $bytes_remaining) $burst_size = $bytes_remaining; // If burst includes the stall point or rate2 point, burst only to that point. if ($do_stall && $stall_at >= $bytes_sent && $stall_at <= $bytes_sent + $burst_size) $burst_size = $stall_at - $bytes_sent; if ($do_rate2 && $rate2_at >= $bytes_sent && $rate2_at <= $bytes_sent + $burst_size) $burst_size = $rate2_at - $bytes_sent; // Send data print fread($file, $burst_size); flush(); ob_flush(); $bytes_remaining -= $burst_size; $bytes_sent += $burst_size; if ($do_rate2 && $rate2_at == $bytes_sent) { $rate = $rate2; $do_rate2 = 0; } if ($do_stall && $stall_at == $bytes_sent) { sleep($stall_time); $do_stall = 0; } else { usleep(100000); // 100ms } } fclose($file); } ?>