admin 发表于 2020-9-27 16:40

php验证文件格式之php_fileinfo扩展(可以防止文件伪装)

php验证文件格式之php_fileinfo扩展(可以防止文件伪装)
在php.ini中开启fileinfo扩展
extension=php_fileinfo.dll注意:开启fileinfo扩展以后,就可以使用finfo_*的函数了
语法代码如下:
<body>
<?php
if(!empty($_POST)) {
        //第一步:创建finfo资源
        $info=finfo_open(FILEINFO_MIME_TYPE);
        //var_dump($info);                //resource(2) of type (file_info)
        //第二步:将finfo资源和文件做比较
        $mime=finfo_file($info,$_FILES['face']['tmp_name']);
        //第三步,比较是否合法
        $allow=array('image/jpeg','image/png','image/gif');        //允许的类别
        echo in_array($mime,$allow)?'合法':'不合法';
}
?>
<form method="post" action="" enctype='multipart/form-data'>
        <input type="file" name="face">
        <input type="submit" name="button" value="上传">
</form>
</body>
页: [1]
查看完整版本: php验证文件格式之php_fileinfo扩展(可以防止文件伪装)