什么是最好的方式来确定文件是否是在PHP中的图像? [英] What is the best way to determine whether or not a file is an image in PHP?

查看:149
本文介绍了什么是最好的方式来确定文件是否是在PHP中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人可以上传文件的服务器。问题是一些文件名被损坏(没有任何扩展名),所以我不能立即确定文件类型。这个问题是两个部分:对于有文件名的文件,确定它是否是图像的最好方法是什么? (只是一个很长的if / else如果列表?)其次,对于没有扩展名的文件,我怎样才能确定它们是否是图像? / div>

您可以使用 exif_imagetype()

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

其中$ type是一个值


  1. IMAGETYPE_GIF

  2. IMAGETYPE_JPEG

  3. IMAGETYPE_PNG

  4. IMAGETYPE_SWF

  5. IMAGETYPE_PSD

  6. IMAGETYPE_BMP

  7. IMAGETYPE_TIFF_II(英特尔字节顺序)
  8. IMAGETYPE_TIFF_MM订单)

  9. IMAGETYPE_JPC

  10. IMAGETYPE_JP2

  11. IMAGETYPE_JPX
  12. IMAGETYPE_SWC

  13. IMAGETYPE_IFF

  14. IMAGETYPE_WBMP li>
  15. 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

  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.

这篇关于什么是最好的方式来确定文件是否是在PHP中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆