ramsey/http-range
Parse, validate, and work with HTTP Range headers in PHP. ramsey/http-range helps you interpret byte ranges, handle partial content requests, and generate correct range responses for downloads, media streaming, and resumable transfers.
Start by requiring the package via Composer:
composer require ramsey/http-range
Then, use the RangeHeaderParser to parse incoming Range headers from requests:
use Ramsey\Http\Range\RangeHeaderParser;
$parser = new RangeHeaderParser();
$ranges = $parser->parse('bytes=0-499, 1000-1499');
// Returns array of RangeInterface objects, e.g. [Range(0, 499), Range(1000, 1499)]
For responding to range requests, create a ContentRange header to send back:
use Ramsey\Http\Range\ContentRange;
$contentRange = new ContentRange(0, 499, 2000); // start, end, total
// Result: "bytes 0-499/2000"
This is ideal for handling resumable uploads or video streaming where clients request specific byte ranges.
fseek(), fread()) and return a 206 Partial Content response with proper Content-Range and Content-Length.Range::validate($ranges, $resourceSize) to detect invalid or impossible requests.bytes=100-50), the parser normalizes or flags them—allow you to implement fallback logic or return 416 Range Not Satisfiable.Range, validate, and short-circuit if ranges are unsatisfiable before heavy processing.RangeHeaderParser to assert correct parsing of edge cases like bytes=-500 (suffix ranges) or bytes=500- (to-end ranges).416 errors or truncated/corrupt responses.Range: bytes=-500 means "last 500 bytes"—validate this correctly: start = max(0, total - 500).ContentRange with explicit start, end, and total.Content-Length as sum of range lengths (not the total file size) and ensure Accept-Ranges: bytes is present in responses.How can I help you explore Laravel packages today?