Can there be a current method to parse the HTTP_RANGE header properly in PHP? Thought I'd request here before re-inventing the wheel.

I'm presently using

preg_match('/bytes=(d+)-(d+)/', $_SERVER['HTTP_RANGE'], $matches)

to parse the header but that doesn't cover all possible values from the header and so i am wondering if there's a function or library that may do that already?

Thanks ahead of time.

Rather use regex to test it before delivering a 416. Then just parse it by overflowing around the comma , and also the hyphen -. I additionally observe that you used d+ inside your regex, but individuals are really not needed. When either from the range indexes is overlooked, it means "first byte" or "last byte". You need to cover that inside your regex too. Also begin to see the Range header within the HTTP spec how you are designed to handle it.

Kickoff example:

if (isset($_SERVER['HTTP_RANGE'])) Asked for Range Not Satisfiable')

        header('Content-Range: bytes */' . filelength) // Needed in 416.

        exit

    

    $ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6))

    foreach ($ranges as $range) finish = $parts[1] // If this sounds like empty or more than than filelength - 1, this ought to be filelength - 1.

        if ($start > $finish) Asked for Range Not Satisfiable')

            header('Content-Range: bytes */' . filelength) // Needed in 416.

            exit

        

        // ...

    



Edit: $start should always be under $finish

Obtained from the PEAR Package HTTP_Download:

function getRanges()



It's also smart to use this packages for that!

There is a snippet applying HTTP range support around the fread() page:

http://world wide web.php.internet/manual/en/function.fread.php#84115