如何检查文件是否为图像? [英] How can I check whether or not a file is an image?

查看:174
本文介绍了如何检查文件是否为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以检查某个文件是否是图像吗?如何在PHP中执行此操作?

Can I check whether or not a certain file is an image? How can do this in PHP?

如果文件不是图像,我想发出警告消息。

If the file is not an image, I want to give an alert message.

推荐答案

getimagesize() ,你可以使用 exif_imagetype()


exif_imagetype()读取第一个图像的字节并检查其签名。

exif_imagetype() reads the first bytes of an image and checks its signature.

找到正确的签名后,将返回相应的常量值,否则返回值为FALSE。返回值与getimagesize()在索引2中返回的值相同,但exif_imagetype()的速度要快得多。

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.

对于这两个函数,如果文件未确定为图像,则返回FALSE。

For both functions, FALSE is returned if the file is not determined to be an image.

这篇关于如何检查文件是否为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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