在php中上传文件时创建文件类型条件

时间:2012-12-20 01:19:43

标签: php

我正在使用这个条件,但它没有工作,因为它总是错误的,即没有文件类型符合条件,所以我fighre必须有语法错误但我无法弄明白。

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 个答案:

答案 0 :(得分: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)而不是否定它。

答案 1 :(得分:5)

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

isset针对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";
}

答案 2 :(得分:2)

在垂直范围内更长,但在我看来更具可读性。

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