I have a sever which people can upload files to. The problem is that some of the filenames are mangled (dont have any extension) and so I cannot immediately determine file type. This question is two part: for the files which do have filenames what is the best way to determine whether or not it is an image? (Just a big long if/else if list?) Secondly, for the files which dont have extensions, how can I determine if they are images?
You can use exif_imagetype()
<?php
$type =exif_imagetype($image);
where $type is a value
- IMAGETYPE_GIF
- IMAGETYPE_JPEG
- IMAGETYPE_PNG
- IMAGETYPE_SWF
- IMAGETYPE_PSD
- IMAGETYPE_BMP
- IMAGETYPE_TIFF_II (intel byte order)
- IMAGETYPE_TIFF_MM (motorola byte order)
- IMAGETYPE_JPC
- IMAGETYPE_JP2
- IMAGETYPE_JPX
- IMAGETYPE_JB2
- IMAGETYPE_SWC
- IMAGETYPE_IFF
- IMAGETYPE_WBMP
- IMAGETYPE_XBM
- IMAGETYPE_ICO
From the manual:
When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE. The return value is the same value that getimagesize()
returns in index 2 but exif_imagetype()
is much faster.