Oihana PHP System

parseRangeHeader.php

Table of Contents

Functions

parseRangeHeader()  : array{0: int, 1: int}|false|null
Parses an HTTP `Range` header into a single satisfiable byte interval.

Functions

parseRangeHeader()

Parses an HTTP `Range` header into a single satisfiable byte interval.

parseRangeHeader(string $rangeHeader, int $fileSize) : array{0: int, 1: int}|false|null

Supports the common single-range forms: bytes=0-499, bytes=500- (open-ended) and bytes=-500 (suffix / last N bytes). Multi-range requests are intentionally not honored and resolve to null (the caller should serve the full content).

Parameters
$rangeHeader : string

The raw Range header value.

$fileSize : int

The total size of the file in bytes.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0
example
parseRangeHeader( 'bytes=0-4'    , 11 ) ; // [0, 4]   -> 206
parseRangeHeader( 'bytes=6-'     , 11 ) ; // [6, 10]  -> 206 (open-ended)
parseRangeHeader( 'bytes=-5'     , 11 ) ; // [6, 10]  -> 206 (last 5 bytes)
parseRangeHeader( 'bytes=0-9999' , 11 ) ; // [0, 10]  -> 206 (end clamped)
parseRangeHeader( 'bytes=99-'    , 11 ) ; // false    -> 416 (unsatisfiable)
parseRangeHeader( 'bytes=0-2,5-' , 11 ) ; // null     -> 200 (multi-range ignored)
parseRangeHeader( ''             , 11 ) ; // null     -> 200 (no range)
Return values
array{0: int, 1: int}|false|null
  • [start, end] (inclusive, clamped) for a satisfiable single range → 206;
  • false when a range is present but unsatisfiable → 416;
  • null when there is no usable single range (absent / malformed / multi-range) → full 200.
On this page

Search results