图像类型验证 PHP


image type validation php

它都在我的表单中工作,但图像验证不起作用。我只想要一个用于图像类型验证的脚本。这是我现在拥有的,但没有工作,我不明白为什么..

enter code here
            if( $_FILES['imagem']['error'] > 0){
                $flag_error=true;        
                $errors['imagens'][0]=true;
            }
            if ($_FILES["imagem"]["size"] > 10000000) {
                $flag_error=true;        
                $errors['tamanhoimagem'][0]=true;
            }
            if($_FILES['imagem']['type']!='image/png' || 'image/jpg') {
                $flag_error=true;        
                $errors['tipodeficheiro'][0]=true;
                }

太微不足道了。

if($_FILES['imagem']['type'] !== 'image/png' && $_FILES['imagem']['type'] !== 'image/jpeg')

或更好:

if(!in_array($_FILES['imagem']['type'], array('image/png', 'image/jpeg')))

您的代码不起作用,因为表达式的计算结果为 ($_FILES['imagem']['type']!='image/png') || 'image/jpg'

|| 是一个布尔运算符,字符串'image/jpg'由编译器转换为布尔true。因此,整个表达式的结果总是true


此外,JPEG图像的MIME类型是image/jpeg,而不是image/jpg

我认为应该是这样的——

if(($_FILES['imagem']['type'] != 'image/png') && ($_FILES['imagem']['type'] !=  'image/jpg')) {
                $flag_error=true;        
                $errors['tipodeficheiro'][0]=true;
                }