Juan Carlos
Juan Carlos

Reputation: 389

Detect link content type with PHP

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.

Upvotes: 2

Views: 555

Answers (3)

Borealid
Borealid

Reputation: 98519

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.

Upvotes: 3

poljpocket
poljpocket

Reputation: 33

Googling a little found me this:

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

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

Upvotes: 2

Bartosz Grzybowski
Bartosz Grzybowski

Reputation: 1179

get_headers - and check for Content-type header.

Upvotes: 1

Related Questions