什么是最好的方式来确定文件是否是在PHP中的图像? [英] What is the best way to determine whether or not a file is an image in PHP?
问题描述
您可以使用 exif_imagetype()
<?php
$ type = exif_imagetype($ image) ;
其中$ type是一个值
- IMAGETYPE_GIF
- IMAGETYPE_JPEG
- IMAGETYPE_PNG
- IMAGETYPE_SWF
- IMAGETYPE_PSD
- IMAGETYPE_BMP
- IMAGETYPE_TIFF_II(英特尔字节顺序)
- IMAGETYPE_TIFF_MM订单)
- IMAGETYPE_JPC
- IMAGETYPE_JP2
- IMAGETYPE_JPX
- IMAGETYPE_SWC
- IMAGETYPE_IFF
- IMAGETYPE_WBMP li>
- IMAGETYPE_ICO
从手册中:
当找到正确的签名时,将返回相应的常量值,否则返回值为FALSE。返回值与索引2中的 getimagesize()
返回的值相同,但是 exif_imagetype()
要快得多。 / p>
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.
这篇关于什么是最好的方式来确定文件是否是在PHP中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!