How to get MIME-type of an image with file_get_contents in PHP

Zwen2012 picture Zwen2012 · Aug 11, 2014 · Viewed 15.6k times · Source

I need to get the MIME type of an image, but I only have the body of the image which I've got with file_get_contents. Is there a possibility to get the MIME type?

Answer

Ram Sharma picture Ram Sharma · Aug 11, 2014

Yes, you can get it like this.

$file_info = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer(file_get_contents($image_url));
echo $mime_type;