public static function readPartialFile($file, $from = 0, $to = 0, $filesize = FALSE, $force = FALSE, $context = NULL) {
        if(!$force) {
            if(!@file_exists($file)) {
                return 404;
            }

            if(!@is_readable($file)) {
                return 403;
            }
        }

        //open file
        if(!($file instanceof SplFileObject)) {
            $file = new SplFileObject($file);
        }

        //get filesize
        $filesize = $file->getSize();

        if(is_array($from)) {
            throw new Exception("Not implemented yet.");
        }

        if($from < 0) {
            $from = $filesize + $from;
        }

        if($to <= 0) {
            $to = $filesize + $to - 1;
        }

        if($from < 0 || $to >= $filesize || $from > $to) {
            throw new Exception("Invalid Range. (filesize: $filesize, from: $from, to: $to)");
        }

        $file->fseek($from);

        $length = $to - $from + 1;

        while($length) {
            $read = ($length > 8192) ? 8192 : $length;
            $length -= $read;
            $content = $file->fread($read);
            if($content === FALSE) {
                return FALSE;
            }
            else {
                print($content);
            }
        }

        return $length;
    }
public static function readPartialFile($file, $from = 0, $to = 0, $filesize = FALSE, $force = FALSE, $context = NULL) {
        if(!$force) {
            if(!@file_exists($file)) {
                return 404;
            }

            if(!@is_readable($file)) {
                return 403;
            }
        }

        //open file
        if(!($file instanceof SplFileObject)) {
            $file = new SplFileObject($file);
        }

        //get filesize
        $filesize = $file->getSize();

        if(is_array($from)) {
            throw new Exception("Not implemented yet.");
        }

        if($from < 0) {
            $from = $filesize + $from;
        }

        if($to <= 0) {
            $to = $filesize + $to - 1;
        }

        if($from < 0 || $to >= $filesize || $from > $to) {
            throw new Exception("Invalid Range. (filesize: $filesize, from: $from, to: $to)");
        }

        $file->fseek($from);

        $length = $to - $from + 1;

        while($length) {
            $read = ($length > 8192) ? 8192 : $length;
            $length -= $read;
            $content = $file->fread($read);
            if($content === FALSE) {
                return FALSE;
            }
            else {
                print($content);
            }
        }

        return $length;
    }
public static function readPartialFile ( $ file , $ from = 0 , $ to = 0 , $ filesize = FALSE , $ force = FALSE , $ context = NULL ) { if ( ! $ force ) { if ( ! @ file_exists ( $ file ) ) { return 404 ; } if ( ! @ is_readable ( $ file ) ) { return 403 ; } } //open file if ( ! ( $ file instanceof SplFileObject ) ) { $ file = new SplFileObject ( $ file ) ; } //get filesize $ filesize = $ file -> getSize ( ) ; if ( is_array ( $ from ) ) { throw new Exception ( "Not implemented yet." ) ; } if ( $ from < 0 ) { $ from = $ filesize + $ from ; } if ( $ to <= 0 ) { $ to = $ filesize + $ to - 1 ; } if ( $ from < 0 || $ to >= $ filesize || $ from > $ to ) { throw new Exception ( "Invalid Range. (filesize: $filesize, from: $from, to: $to)" ) ; } $ file -> fseek ( $ from ) ; $ length = $ to - $ from + 1 ; while ( $ length ) { $ read = ( $ length > 8192 ) ? 8192 : $ length ; $ length -= $ read ; $ content = $ file -> fread ( $ read ) ; if ( $ content === FALSE ) { return FALSE ; } else { print ( $ content ) ; } } return $ length ; }
Like PHPs readfile, but the range of the output can be specified

<code>
use Loops\Misc;
Misc::readpartialfile("example.png", 500, 1000);
</code>

Instead of integer ranges an array containing a Content-Range header can be passed.
The values found in this header will be used to output the partial file, making it
possible to use this function in combination with Loops\Misc::servefile

<code>
use Loops\Misc;
$headers = Misc::servefile("example.png", TRUE, TRUE, TRUE);
foreach($headers as $line) {
header($line);
}
Misc::readpartialfile("example.png", $headers);
exit;
</code>

@param string $file The physical location of the file (must be understood by fopen)
@param int|array $from Start file output from this offset or an array containing a Content-Range header line (negative value: n bytes from EOF)
@param int $to Stop file output including this offset (0: EOF, negative value: n bytes from EOF)
@param resource $context The context passed to fopen.
Like PHPs readfile but the range of the output can be specified