开发者

File type PHP validation

What I am doing wrong with the code , it always shows 'File types .doc,.docx,.odt and max file size 2mb supported 'even if the file type is .doc format and size is 0kb.

if(($_FILES["fi开发者_StackOverflow中文版le"]["type"] != 'application/octet-stream') ||
            ($_FILES["file"]["type"] != 'application/vnd.oasis.opendocument.text')||
            ($_FILES["file"]["type"] != 'application/msword')||
            ($_FILES["file"]["size"] > 200000))
        {
            //echo $_FILES["file"]["size"];
            //echo $_FILES["file"]["type"];
            $this->assign_values('msg',"File types .doc,.docx,.odt and max file size 2mb supported");
            //echo "Error: " . $_FILES["file"]["error"] . "<br />";
        }


You need to change the || to && between the file types. Or even better check the type against a list of allowed types:

$allowedTypes = array('application/octet-stream', 'application/vnd.oasis.opendocument.text', 'application/msword');

if(!in_array($_FILES["file"]["type"], $allowedTypes) || $_FILES["file"]["size"] > 200000)
        {
            //echo $_FILES["file"]["size"];
            //echo $_FILES["file"]["type"];
            $this->assign_values('msg',"File types .doc,.docx,.odt and max file size 2mb supported");
            //echo "Error: " . $_FILES["file"]["error"] . "<br />";
        }


You are using OR, which means 1 of the parameters you gave has to be true.

Since you are checking the type 3 times on different values, it is bound to always return true and thus trigger the if.


You are using OR( || ) which if any of the one statement is true the code will execute

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜