如何在php上传之前验证图像文件?

时间:2018-02-21 07:07:16

标签: php file-upload

我正在尝试上传一个文件,我想只允许png文件。对于验证,我会检查文件的扩展名是否为.png。但在我的情况下,如果用户将文件扩展名从gif更改为png,那么如何在上传php之前验证图像。

1 个答案:

答案 0 :(得分:2)

跟着这个 http://php.net/manual/en/function.exif-imagetype.php

你会找到方法

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

<?php
if (exif_imagetype('image.gif') != IMAGETYPE_GIF) {
    echo 'The picture is not a gif';
}
?>