0

我正在使用这个条件,但它不起作用,因为它总是错误的,即没有文件类型符合条件,所以我认为语法上一定有错误,但我无法弄清楚。

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";
}    
4

3 回答 3

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";
} 

我假设有效意味着这些类型中的任何一种,因此您检查其中任何一种(使用or)然后否定它。

于 2012-12-20T01:21:49.397 回答
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";
}
于 2012-12-20T01:31:15.337 回答
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";
}
于 2012-12-20T01:31:04.293 回答