0

I want to find the file type hosted in external server.I tried get_headers() function but it returns an array of headers i need the content type only.The array is in different order according to the file so it can't be identified using position of array element.Please give me a solution.

Rahul R
  • 209
  • 2
  • 11

1 Answers1

6

If you set the format parameter to 1, it'll make an associative array of the headers:

$headers = get_headers($url, 1);
$content_type = $headers['Content-Type'];

See the documentation of get_headers for more information.

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
  • Why is **my** before variables? – Rolice Jul 06 '13 at 09:33
  • @Rolice: Hah, my bad. I code too much perl, is why. Fixed. :) – Sebastian Paaske Tørholm Jul 06 '13 at 09:34
  • Yep, I thought it is Perl... but I doubted if it is something new I missed around PHP 5.4 :) – Rolice Jul 06 '13 at 09:37
  • 1
    [`array_change_key_case`](http://php.net/array_change_key_case) is advisable, because `get_headers()` returns the plain results. [Headers are *case-insensitive*](http://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive) however, and while almost never an issue (Apache normalizes some), it's not guaranteed that it's labeled exactly `Content-Type`. `CONTENT-TYPE` and `content-type` or `cONTent-tYpe` are also permitted. – mario Jul 06 '13 at 10:14