0

我有 PHP 表单,用户可以在其中上传图像。我检查此表单是否存在其他几个问题,例如空字段,每个问题都有自己的错误消息。如果有多个错误,则 de form 不会发送数据并且用户会看到错误消息。到目前为止,一切都很好!现在我想在图像不是 PNG、JPG 或 GIF 或大于 250k 时显示错误消息。我写了一个代码,这似乎没有做任何事情!我可以将整个表单留空并将 .MP3 设置为“要上传的文件”,所有错误消息都会出现,除了图像的错误消息。怎么了?这是我的代码:

$aErrors = array();

$filecheck = basename($_FILES['bedrijfslogo']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));

if (!(($ext != "jpg" && $ext != "gif" && $ext != "png") || ($_FILES["bedrijfslogo"]["type"] != "image/jpeg" && $_FILES["bedrijfslogo"]["type"] != "image/gif" && $_FILES["bedrijfslogo"]["type"] != "image/png") ||    ($_FILES["bedrijfslogo"]["size"] > 250000))){

    $aErrors['bedrijfslogo'] = 'Uw bedrijfslogo is te groot of niet het juiste formaat';
  }

以及 HTML 表单中的代码:

<tr>
<td class="first">Bedrijfslogo:</td>
<td><input tabindex="8" type="file" name="bedrijfslogo" value="<?php echo isset($_POST['bedrijfslogo'])?$_POST['bedrijfslogo']:""; ?>" class="wizardinput" style="background-color:white;"></td>
</tr>

伙计们怎么了?提前非常感谢!

4

1 回答 1

2

你的逻辑有点混乱

if (!(($ext != "jpg" && $ext != "gif" && $ext != "png") || ($_FILES["bedrijfslogo"]["type"] != "image/jpeg" && $_FILES["bedrijfslogo"]["type"] != "image/gif" && $_FILES["bedrijfslogo"]["type"] != "image/png") ||    ($_FILES["bedrijfslogo"]["size"] > 250000))){
    $aErrors['bedrijfslogo'] = 'Uw bedrijfslogo is te groot of niet het juiste formaat';
}

尝试简化它

if (!preg_match("/^jpg|gif|png$/", $ext) || !preg_match("/^image\/(jpeg|gif)$/", $_files["bedrijfslogo"]["type"]) || $_FILES["bedrijfslogo"]["size"] > 250000) {
    //code here
}
于 2012-07-26T19:33:43.897 回答