0

I am using this condition, but it's not working as it is always false i.e no file types meet the condition, so i fighre there must be an error in syntax but I can't figure it out.

if (!($_FILES["uploaded"]["type"] == "video/mp4")
&& (!$_FILES["uploaded"]["type"] == "video/flv")
&& (!$_FILES["uploaded"]["type"] == "video/webm" )
&& (!$_FILES["uploaded"]["type"] == "video/ogg" ))

{
   $message="not an accepted file format";
}    

3 Answers 3

5

A common case for in_array:

$type          = $_FILES["uploaded"]["type"];
$allowedTypes  = ["video/mp4", "video/flv", "video/webm", "video/ogg"];
$isRefusedType = !in_array($type, $allowedTypes);
if ($isRefusedType) {
    $message = "not an accepted file format";
}

Or isset against a flipped array:

$type          = $_FILES["uploaded"]["type"];
$allowedTypes  = array_flip(["video/mp4", "video/flv", "video/webm", "video/ogg"]);
$isRefusedType = !isset($allowedTypes[$type]);
if ($isRefusedType) {
    $message = "not an accepted file format";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yep, I'd definitely go for in_array myself.
Anything not to use a switch, eh? ;) The value here, is that the disallowed types can be loaded from configuration.
Yes both are pretty similar in this case. I first tended to write an in_array example but found array keys worth to show, too. in_array examples exist, but can't find it yet. - Okay, added and also seeing it was the wrong logic, not refused types, but allowed ones.
5
if ( !($_FILES["uploaded"]["type"] == "video/mp4"
       || $_FILES["uploaded"]["type"] == "video/flv"
       || $_FILES["uploaded"]["type"] == "video/webm"
       || $_FILES["uploaded"]["type"] == "video/ogg") )
{
   $message="not an accepted file format";
} 

I assume valid means any of these types, so you check for any of these (using or) and than negate it.

7 Comments

was typing this up when you posted +1
Shouldn't the ! character be outside the parentheses?
no because then if it's not mp4 but it is flv then the message will come up
@user1559811 I misread your parentheses and forgot to remove the !'s, please check the updated answer.
@jeroen looks better without the parentheses!
|
2

Longer on a vertical scale, but in my opinion more readable.

switch ($_FILES["uploaded"]["type"]) {
    case "video/webm":
    case "video/mp4":
    case "video/flv":
    case "video/ogg":
        $message = "Okie dokie";
        break;
    default:
        $message = "Invalid format";
}

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.