2

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["file"]["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 />";
        }
1
  • Try removing the components of your if clause until you see a change. Commented Aug 1, 2011 at 7:57

3 Answers 3

4

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 />";
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Oh thanks. Its working now . Can anyone tell me whts going wrong in my code ?
Well your if statement in written language is somethin like "If type is not A or type is not B or type is not C or size is greater than 20000". But the type can only have one value. So if it is "A" the check for "type is not B" returns true and your if "matches" also it has the allowed type "A".
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.