How to get MIME type of a file in PHP 5.5?

Jitendra Yadav picture Jitendra Yadav · Apr 25, 2014 · Viewed 72.7k times · Source

I am using mime_content_type() in PHP 5.5 to get a MIME type, but it throws fatal: error function not found.

How can I achieve this on PHP 5.5?

Answer

Shankar Damodaran picture Shankar Damodaran · Apr 25, 2014

Make use of the finfo() functions.

A simple illustration:

<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, "path/to/image_dir/image.gif");
finfo_close($finfo);

OUTPUT :

image/gif

Note : Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension.