0
if(isset($_FILES['avatar']) && !empty($_FILES['avatar']['name'])){              
    if($_FILES['avatar']['type'] != "image/gif" || $_FILES['avatar']['type'] != "image/jpeg" || $_FILES['avatar']['type'] != "image/jpg" || $_FILES['avatar']['type'] != "image/pjpeg" || $_FILES['avatar']['type'] != "image/x-png" || $_FILES['avatar']['type'] != "image/png"){
        $errors[] = 'File does not have the right mime type.';
        $errors[] = '(This avatar has the mime type '. $_FILES['avatar']['type'] .')';
    }

    if($_FILES['avatar']['size'] > 40960){
        $errors[] = 'File is too large, max 40 kb.';
        $errors[] = 'This is '. $_FILES['avatar']['size'];
    }
}

你好再次stackoverflow!

我让我的用户在注册时决定他们是想立即上传他们的头像,还是稍后再上传。

我目前正在尝试的是上传一个 800b 的 gif 文件。但是在上传时,我收到文件与允许的 mime 类型不匹配的错误。我在这里做什么?

4

1 回答 1

1

替换||&&

想一想,||意思是or。如果 type 不是“image/gif”或 type 不是“image/jpeg”,或者......其中一个为真,您将输入if。例如,如果类型是“image/gif”,那么它不是“image/jpeg”,您仍然会输入。

更优雅地替换为in_array. 例如,

if(!in_array($type, ["image/gif", "image/jpeg"])) // and so on
    $errors[] = 'File does not have the right mime type.';
于 2013-08-25T16:02:21.600 回答