What is the best way to determine whether or not a file is an image in PHP?

前端 未结 12 496
有刺的猬
有刺的猬 2021-01-22 07:25

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.

12条回答
  •  再見小時候
    2021-01-22 08:03

    You can use exif_imagetype()

    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.

提交回复
热议问题