Is there an existing way to parse the HTTP_RANGE
header correctly in PHP? Thought I'd ask here before re-inventing the wheel.
I am currently using
preg_match('/bytes=(\d+)-(\d+)/', $_SERVER['HTTP_RANGE'], $matches);
to parse the header but that does not cover all possible values of the header so I am wondering if there is a function or library that can do this already?
Thanks in advance.
Rather use regex to test it before sending a
416
. Then just parse it by exploding on the comma,
and the hyphen-
. I also see that you used\d+
in your regex, but those are actually not required. When either of the range indexes is omitted, then it just means "first byte" or "last byte". You should cover that in your regex as well. Also see the Range header in the HTTP spec how you're supposed to handle it.Kickoff example:
Edit: $start must always be less than $end
There's a snippet implementing HTTP range support on the
fread()
page:http://www.php.net/manual/en/function.fread.php#84115
Taken from the PEAR Package HTTP_Download:
It is also a good idea to use this packages for stuff like this!