【问题标题】:image type validation php [closed]图像类型验证php [关闭]
【发布时间】:2015-02-12 23:06:39
【问题描述】:

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

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;

                }

【问题讨论】:

    标签: php image validation types


    【解决方案1】:

    如此琐碎。

    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'

    || 是一个boolean operator,而字符串'image/jpg' 是编译器的converted to boolean 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; }
    • 我得到了这个但仍然无法正常工作,说该文件不受支持:/
    • 也许应该是image/jpeg(注意jpeg中的e)?
    • 非常感谢你!我浪费了很多时间试图解决此页面上的错误,这是最后一个!真的很欣赏这个有用的网站。谢谢你
    【解决方案2】:

    我觉得应该是这样-

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2011-01-03
      • 2012-06-18
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多