我正在使用get_headers(),我想返回内容长度。在下面的数组中,内容长度是键号9,但我发现内容长度并不总是键号9.我不确定为什么它不总是相同的键?如何搜索数组并始终返回内容长度,无论它是什么键?因为我在函数中使用它,所以文件的实际文件大小每次都不同。我的示例代码如下。提前谢谢。
Array ( [0] => HTTP/1.0 200 OK [1] => Server: Apache [2] => Pragma: public [3] => Last-Modified: Sun, 01 Apr 2012 07:59:46 GMT [4] => ETag: "1333267186000" [5] => Content-Type: text/javascript [6] => Cache-Control: must-revalidate, max-age=0, post-check=0, pre-check=0 [7] => Expires: Sun, 01 Apr 2012 10:27:26 GMT [8] => Date: Sun, 01 Apr 2012 10:27:26 GMT [9] => Content-Length: 31650 [10] => Connection: close [11] => Set-Cookie: JSESSIONID=M0V4NTTVSIB4WCRHAWVSFGYKE2C0UIV0; path=/ [12] => Set-Cookie: DYN_USER_ID=2059637079; path=/ [13] => Set-Cookie: DYN_USER_CONFIRM=6c6a03988da3de6d704ce37a889b1ec8; path=/ [14] => Set-Cookie: BIGipServerdiy_pool=637871882.20480.0000; path=/ )
function headInfo($url) {
$header = get_headers($url);
$fileSize = $header[9];
return array($header, $fileSize);
}
答案 0 :(得分:4)
第二个参数:
如果可选格式参数设置为非零,则get_headers()会解析响应并设置数组的键。
所以,只需传递1作为第二个参数,$ header ['Content-Length']就是你所追求的。
答案 1 :(得分:1)
get_headers($url,true);
答案 2 :(得分:0)
如果查看get_headers()函数文档,您可能会注意到,有第二个(默认)参数可用于指定输出格式。
$hdr = get_headers($url, 1);
echo("Content-length: " . $hdr["Content-length"]);
1 - 表示该函数将返回关联数组。
答案 3 :(得分:0)
基本上
array get_headers ( string $url [, int $format = 0 ] )
示例:
get_headers($url,true);
将为您提供关联数组
详细
<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
这将产生
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)
希望它有助于