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?
-
http://stackoverflow.com/questions/676949/best-way-to-determine-if-a-url-is-an-image-in-php – Mob Dec 06 '11 at 20:11
12 Answers
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.

- 1,755
- 14
- 18
-
1
-
You need fallback. exif_imagetype() is not available on all systems, it seems – rosell.dk Mar 14 '19 at 10:31
You can use getimagesize It does not require the GD image library and it returns same information about image type. http://it2.php.net/manual/en/function.getimagesize.php

- 726
- 6
- 10
-
You can use @getimagesize('name-of-file') to suppress WARNING for non-image file – InuYaksa Dec 06 '11 at 20:12
-
@chacham - It's not an error, it's only a warning. The function returns false if the file is invalid, one should check with `getimagesize($filename) === false`. – martinstoeckli Dec 06 '11 at 20:29
-
Which method requires GD? According to [this link](https://www.macs.hw.ac.uk/~hwloidl/docs/PHP/function.exif-imagetype.html), exif_imagetype does not require GD, but it requires that PHP is compiled using --enable-exif. – rosell.dk Mar 14 '19 at 10:09
If you have the GD2 extension enabled, you could just use that to load the file as an image, then if it returns invalid you can catch the error and return FALSE, otherwise return TRUE.

- 558
- 1
- 7
- 19
You have two options here, one's simple and pre-built with some shortfalls, the other is complex and requires math.
PHP's fileinfo can be used to detect file types based on the file's actual header information. For instance, I just grabbed your gravitar:
But the actual code is this:
‰PNG
IHDR szzô
IDATX…—OL\UÆZÀhëT)¡ c•1T:1‘Š‘.Ú(]4†A“ÒEY˜à.................................
So, even without the file name I could detect it quite obviously. This is what the PHP Fileinfo extension will do. Most PNG and JPG files tend to have this header in them, but this is not so for every single file type.
That being said, fileinfo is dead simple to use, from the manual:
$fi = new finfo(FILEINFO_MIME,'/usr/share/file/magic');
$mime_type = $fi->buffer(file_get_contents($file));
Your other option is more complex and it depends on your own personal ambitions, you could generate a histogram and profile files based on their content.
Something like this looks like a GIF file:
And something like this looks like a TIFF file:
From there you'd need to generate a model over multiple types of files for what the histogram of each type should be, and then use that to guess. This is a good method to use for files that don't really have those "magic headers" that can be read easily. Keep in mind, you'll need to learn some math and how to model an average histogram function and match them against files.

- 20,537
- 15
- 80
- 120
-
1I recommend you post this answer on the 'original' question too, because it looks a lot better than the existing ones – Ivo Flipse Dec 23 '11 at 22:28
You can try to load the image into PHP's GD library, and see if it works.
$file = file_get_contents('file');
$img = imagecreatefromstring($file);
if($img === FALSE){
// file is NOT an image
}
else{
// file IS an image
}

- 223,194
- 41
- 299
- 337
Look at image magic identify. http://www.imagemagick.org/script/identify.php
The php wrapper is here: http://www.php.net/manual/en/function.imagick-identifyimage.php
Or if you just want to validate that it's an image (and don't care about the meta data): http://www.php.net/manual/en/function.imagick-valid.php

- 10,351
- 10
- 38
- 53
You can use the Fileinfo extension: http://www.php.net/manual/en/function.finfo-file.php
finfo_file() uses magic bytes and does not have to load the whole image into memory. The result is a string with the corresponding MIME type, e.g.:
- text/html
- image/gif
- application/vnd.ms-excel

- 6,961
- 3
- 24
- 26
If you need a fast solution, use imagesx() and imagesy(). There is also a fast way to check large image file dimensions, by reading just a small amount of data from the file header. Explained in more detail in the following url:
http://hungred.com/useful-information/php-fastest-image-width-height/

- 3,171
- 3
- 23
- 32
The type of the image is typically going to be able to be inferenced from the header information of the file.

- 1,214
- 7
- 9
For the first question is extension is known you could use the PHP function in_array()
Documentation

- 15,435
- 14
- 53
- 83