在 PHP 中解析 HTTP_RANGE 标头
PHP 中是否有正确解析 HTTP_RANGE
标头的现有方法?我想在重新发明轮子之前我应该在这里问一下。
我目前正在使用
preg_match('/bytes=(\d+)-(\d+)/', $_SERVER['HTTP_RANGE'], $matches);
解析标头,但这并没有涵盖标头的所有可能值,所以我想知道是否有一个函数或库可以做到这一点?
提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而是在发送 测试它>
416
。然后只需通过在逗号,
和连字符-
上展开来解析它。我还看到您在正则表达式中使用了\d+
,但这些实际上是不需要的。当省略任一范围索引时,它仅表示“第一个字节”或“最后一个字节”。您也应该在正则表达式中涵盖这一点。另请参阅 HTTP 规范中的范围标头应该可以处理它。启动示例:
编辑:$start 必须始终小于 $end
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
取自 PEAR 包 HTTP_Download:
使用 this 此类内容的包!
Taken from the PEAR Package HTTP_Download:
It is also a good idea to use this packages for stuff like this!
fread()
页面上有一个实现 HTTP 范围支持的代码片段:http://www.php.net/manual/en/function.fread.php#84115
There's a snippet implementing HTTP range support on the
fread()
page:http://www.php.net/manual/en/function.fread.php#84115