How to get the mime type of a remote file in php (with redirects)

Just in case it’s of any use to anyone else out there, here’s a function that can get the mime type of a remote file. It will also follow any redirects that are in place.

function get_url_mime_type($url)
{

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
return curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

}



Check out my tech startup podcast TechZing. Maximise your downtime by listening to us while you code, commute, work-out or do the dishes!