我有一个PHP表单,用户可以在其中上传图像。我检查了这个表单中的其他几个问题,比如空字段,每个问题都会得到自己的错误消息。如果存在多个错误,则反表单不会发送数据,并且用户会看到一条错误消息。到目前为止,一切都很好!现在,当图像不是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>
怎么了,伙计们?非常感谢!
您的逻辑有点混淆
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
}