2

How can I detect the content type of a link?

Users can upload links into thee web. And I'd need to save the link type (a video, a image...) in the DB (mysql).

I've found one answer (using the HTTP HEAD) with Java: Selenium 2: Detect content type of link destinations . However I'm using PHP.

Thanks.

Community
  • 1
  • 1
Juan Carlos
  • 389
  • 3
  • 12

3 Answers3

3

That solution applies, in the abstract, to PHP as well.

The way you detect a link target's content type is by performing an HTTP HEAD request (such as with the curl extension for PHP, or the get_headers function) and then reading the returned Content-Type header.

Make sure to fail gracefully if the remote end is inaccessible.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • note that `get_headers` will do a GET request unless you change the method via a custom stream context – Gordon Feb 20 '12 at 17:24
2

Googling a little found me this:

http://php.net/manual/en/function.getallheaders.php

You then can read the "content-type" header entry.

poljpocket
  • 33
  • 6
  • note that `get_headers` will do a GET request unless you change the method via a custom stream context – Gordon Feb 20 '12 at 17:26
1

get_headers - and check for Content-type header.

Bartosz Grzybowski
  • 1,149
  • 8
  • 18
  • note that `get_headers` will do a GET request unless you change the method via a custom stream context – Gordon Feb 20 '12 at 17:24