I am using pathinfo to find the extension of a file type.

pathinfo( $file, PATHINFO_EXTENSION );

Looking at the PHP manual, it doesn't clarify how this function gets this information. Does this check the image for its MIME type and return that or does it just find the extension by using explode() or such?

If the MIME is not returned, what is the most 'modern' way of checking a files MIME type?

And is it a wise to compare the MIME with the extension?

1

Best Answer


You can use finfo_file and finfo_open to check MIME of files.

<?php$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extensionforeach (glob("*") as $filename) {echo finfo_file($finfo, $filename) . "\n";}finfo_close($finfo);?>

The above example will output something similar to:

text/html