What is the best way to determine whether or not a

2019-02-26 01:54发布

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?

12条回答
在下西门庆
2楼-- · 2019-02-26 02:13

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:

enter image description here

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:

enter image description here

And something like this looks like a TIFF file:

enter image description here

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.

查看更多
虎瘦雄心在
3楼-- · 2019-02-26 02:14

You can use exif_imagetype()

<?php
    $type =exif_imagetype($image);

where $type is a value

  1. IMAGETYPE_GIF
  2. IMAGETYPE_JPEG
  3. IMAGETYPE_PNG
  4. IMAGETYPE_SWF
  5. IMAGETYPE_PSD
  6. IMAGETYPE_BMP
  7. IMAGETYPE_TIFF_II (intel byte order)
  8. IMAGETYPE_TIFF_MM (motorola byte order)
  9. IMAGETYPE_JPC
  10. IMAGETYPE_JP2
  11. IMAGETYPE_JPX
  12. IMAGETYPE_JB2
  13. IMAGETYPE_SWC
  14. IMAGETYPE_IFF
  15. IMAGETYPE_WBMP
  16. IMAGETYPE_XBM
  17. 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.

查看更多
We Are One
4楼-- · 2019-02-26 02:15

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.

查看更多
祖国的老花朵
5楼-- · 2019-02-26 02:15

For the first question is extension is known you could use the PHP function in_array() Documentation

查看更多
走好不送
6楼-- · 2019-02-26 02:16

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

查看更多
混吃等死
7楼-- · 2019-02-26 02:16

exif_imagetype() might work

make sure you have exif enabled.

查看更多
登录 后发表回答