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.
$totalSize parameters and getTotalSize() methods are now typed as float | int | string instead of mixedgetStart(), getEnd(), and getLength() methods on UnitRangeInterface now return float | int | string instead of mixedramsey/http-range provides functionality for parsing HTTP range headers for a variety of range units. Out-of-the-box, ramsey/http-range supports bytes ranges as defined in RFC 7233 § 2.1, as well as basic support for generic range units. The library provides interfaces, abstract classes, and factories, allowing creation of other range units.
ramsey/http-range is designed to be used with PSR-7 RequestInterface objects. Assuming that $request in the following example conforms to this interface, the following example shows how to use this library to parse an HTTP Range header.
The following HTTP request uses a Range header to request the first 500 bytes of the resource at /image/1234.
GET /image/1234 HTTP/1.1
Host: example.com
Range: bytes=0-499
When receiving a request like this, you can parse the Range header using the following.
use Ramsey\Http\Range\Exception\NoRangeException;
use Ramsey\Http\Range\Range;
$filePath = '/path/to/image/1234.jpg';
$filePieces = [];
$range = new Range($request, filesize($filePath));
try {
// getRanges() always returns an iterable collection of range values,
// even if there is only one range, as is the case in this example.
foreach ($range->getUnit()->getRanges() as $rangeValue) {
$filePieces[] = file_get_contents(
$filePath,
false,
null,
$rangeValue->getStart(),
$rangeValue->getLength()
);
}
} catch (NoRangeException $e) {
// This wasn't a range request or the `Range` header was empty.
}
How can I help you explore Laravel packages today?