<?php

function send_file($location$filename=false$mimeType='application/octet-stream')
{
    if (!
file_exists($location)) {
        
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
        return 
404;
    }

    if (
$filename===false) {
        
$filename pathinfo($locationPATHINFO_BASENAME);
    }

    
$size filesize($location);
    
$time date('r',filemtime($location));

    
$fm = @fopen($location,'rb');
    if (!
$fm) {
        
header("{$_SERVER['SERVER_PROTOCOL']} 500 Internal server error");
        return 
500;
    }

    
$begin 0;
    
$end $size;

    if (isset(
$_SERVER['HTTP_RANGE'])) {
        if (
preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i'$_SERVER['HTTP_RANGE'], $matches)) {
            
$begin intval($matches[0]);
            if (!empty(
$matches[1])) {
                
$end=intval($matches[1]);
            }
        }
    }

    if (
$begin>|| $end<$size) {
        
$status 206;
        
header("{$_SERVER['SERVER_PROTOCOL']} 206 Partial Content");
    } else {
        
$status 200;
        
header("{$_SERVER['SERVER_PROTOCOL']} 200 OK"); 
    }

    
header("Content-Type: {$mimeType}");
    
header('Accept-Ranges: bytes');
    
header('Content-Length:' . ($end-$begin));
    
header("Content-Range: bytes {$begin}-{$end}/{$size}");
    
header("Content-Disposition: inline; filename={$filename}");
    
header('Content-Transfer-Encoding: binary');
    
header("Last-Modified: {$time}");
    
header('Connection: close'); 

    
// IE sucks (SSL + IE)
    
header('Cache-Control: maxage=1');
    
header('Pragma: public');

    
$cur $begin;
    
fseek($fm$begin0);

    while (!
feof($fm) && $cur<$end && (connection_status()==0)) {
        print 
fread($fmmin(1024*16,$end-$cur));
        
$cur += 1024*16;
    }

    return 
$status;
}


send_file('big_file.bmp');
sleep(10);

?>