我有一个 PHP 脚本,可以按字节范围提供 PDF 文件的各个部分。
如果收到 HTTP HEAD 请求,它应该发回 header (包括 PDF 文件大小)而不是实际文件内容。我试过这个:
header('HTTP/1.1 200 OK');
header('Content-Type: application/pdf');
header('Accept-Ranges: bytes');
header('Content-Length: '.filesize($Pathname));
die;
问题是某些东西(我假设网络服务器 == LiteSpeed)将 Content-Length header 替换为 Content-Length: 0
- 这违背了整个目的。
谁能建议我应该做什么?谢谢
最佳答案
来自 w3c 超文本传输协议(protocol) -- HTTP/1.1:
When a Content-Length is given in a message where a message-body is allowed, its field value MUST exactly match the number of OCTETs in the message-body. HTTP/1.1 user agents MUST notify the user when an invalid length is received and detected.
和:
The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.
所以,我想,如果您向服务器发送真正的 HEAD 请求,您的代码将正常工作。
关于PHP 响应 HEAD 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41462493/