0

假设一个文件保存在磁盘上:

HTTP/1.1 200 OK
Date: Thu, 28 Jun 2012 22:11:14 GMT
Pragma: no-cache
Cache-Control: must-revalidate, no-store
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Length: 1234
Connection: close
Content-Type: text/html

<html ....

php中是否有任何内置函数可以输出文件,同时识别其中的标题并正确发送它们?

4

2 回答 2

1

不,没有。你必须自己实现一个。

于 2012-10-13T21:12:55.350 回答
0

它不存在,但您可以使用以下

$file = "file.html";
var_dump(fileHeaders($file));

输出

array
  0 => string 'HTTP/1.1 200 OK' (length=15)
  1 => string 'Date: Thu, 28 Jun 2012 22:11:14 GMT' (length=35)
  2 => string 'Pragma: no-cache' (length=16)
  3 => string 'Cache-Control: must-revalidate, no-store' (length=40)
  4 => string 'Expires: Thu, 01 Jan 1970 00:00:00 GMT' (length=38)
  5 => string 'Content-Length: 1234' (length=20)
  6 => string 'Connection: close' (length=17)
  7 => string 'Content-Type: text/html' (length=23)
  8 => string '' (length=0)

使用的功能

function fileHeaders($file) {
    $content = file_get_contents($file, null, null, 0, 1000);
    $content = trim($content);
    if (stripos($content, "HTTP") === 0) {
        return array_map("trim", explode("\n", strtok($content, "<")));
    }
    return false;
}
于 2012-10-13T21:17:58.767 回答