The PHP Function Image_Type_To_Mime_Type()
PHP is not only for creating HTML but also has a lot of functions to manipulate image files. Using these functions you can output image streams to browsers or even create thumbnail images of larger files. Most of these image functions are based on the GD library and require the GD extension to be compiled into PHP. However, PHP has a few inbuilt functions that do not require this extension. The php function image_type_to_mime_type() is one such inbuilt function. This function takes a constant value (IMAGETYPE_XXX) as input and returns a MIME type string which is used to identify the file format of the image. This function is useful if you do not want to use the dreaded header() call for determining the file format.
The php function exif_imagetype() is another good alternative to image_type_to_mime_type() but it differs in its approach to determine the file type. This function reads the metadata stored in JPEG and TIFF images. It can be called with three optional arguments, $arrays, $thumbnail and the $filename of the image. $arrays indicates if the metadata sections should be read as an array, while the $thumbnail parameter is a Boolean which specifies whether or not to read a thumbnail image as well. This function does not require the GD library but it does depend on zlib, so you will need to ensure that this is included in your PHP configure line. It will return FALSE if the file is not an image or the file extension does not match the MIME type.