0

I am using get_headers() and I want to return the content length. In the array below content length is key number 9, however I find that the content length is not always key number 9. I'm not sure why it is not always the same key? How do I search an array and always return the content length no matter what key it is? As I am using this in a function so the actual filesize of the file will be differen't each time. My example code is below. Thanks in advance.

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);
}
Ben Paton
  • 1,432
  • 9
  • 35
  • 59

4 Answers4

4

http://php.net/get_headers

The second parameter:

If the optional format parameter is set to non-zero, get_headers() parses the response and sets the array's keys.

So, just pass 1 as the second parameter, and $header['Content-Length'] will be what you've after.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • Spot on, make it $header = get_headers($url, 1) and you get back an associative array. Then you can look up on the key value. – Lee Davis Apr 01 '12 at 10:43
1
get_headers($url,true);

dynamic
  • 46,985
  • 55
  • 154
  • 231
0

Basically

array get_headers ( string $url [, int $format = 0 ] )

Example:

get_headers($url,true);

will give you associative array

In detail

 <?php
 $url = 'http://www.example.com';

 print_r(get_headers($url));

 print_r(get_headers($url, 1));
 ?>

This will produce

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
)

Hope it Helps

Siv
  • 1,026
  • 19
  • 29
0

If you look at the get_headers() function documentaion, you may notice, there is second (default) parameter which can be used to specify output format.

$hdr = get_headers($url, 1);
echo("Content-length: " . $hdr["Content-length"]);

1 - means the function will return associative array.

heximal
  • 10,327
  • 5
  • 46
  • 69