Reputation: 327
I have PHP form where a user can upload an image. I check this form for several other problems like empty fields and every problem gets their own error message. If there is more than one error, de form doesn't send the data and the user sees an error message. So far, so good! Now I want to show an error message when the image isn't a PNG, JPG or GIF or is larger than 250k. I've written a code, that doesn't seem to do anything! I can leave the entire form blank and set a .MP3 as 'file to upload', all the error messages appear, excep the one for the image. What's going wrong? Here is my code:
$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';
}
And the code inside the HTMl form :
<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>
What's going wrong guys? Big thanks in advance!
Upvotes: 0
Views: 90
Reputation: 7040
Your logic is a little confusing
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';
}
Try simplifying it
if (!preg_match("/^jpg|gif|png$/", $ext) || !preg_match("/^image\/(jpeg|gif)$/", $_files["bedrijfslogo"]["type"]) || $_FILES["bedrijfslogo"]["size"] > 250000) {
//code here
}
Upvotes: 2