How to check whether a MIME type is of JPG, PNG, BMP or GIF?

Lajos Arpad picture Lajos Arpad · May 15, 2013 · Viewed 41.4k times · Source

I have written this code:

$filename = "some/path/where/the/file/can/be/found.some_extension";
$buffer = file_get_contents($filename);
$finfo = new finfo(FILEINFO_MIME_TYPE);
var_dump($finfo->buffer($buffer));
finfo_close($finfo);

Possible outputs were of:

"image/jpeg", "image/png", "image/gif", "image/x-ms-bmp"

I would like to know, what are the possible outputs of $finfo->buffer($buffer), if the file is a png, gif, bmp, or jpg?

I have seen here a returnMIMEType function, which, for instance will not detect "image/x-ms-bmp" to be a bmp.

Answer

Lajos Arpad picture Lajos Arpad · May 20, 2013

We can view the possible MIME types of file extensions by searching for the file extension at the link provided by Pitchinnate. For instance BMP MIME types can be found at: filext.com/file-extension/BMP