0

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!

2
  • You can post the complete form? Commented Jul 26, 2012 at 19:26
  • confused, although I'm making an assumption but given your form field name but shouldn't it be && $_FILES["bedrijfslogo"]["size"] > 250000 your logic checks if it's an images or if it's less than a certain size Commented Jul 26, 2012 at 19:40

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, that looks a lot simpeler indeed. However, using your code, I always get the error message, also when I don't select any file to upload?
@user1555076 Remove the parentheses from jpg|gif|png. Edited for correctness.
@user1555076 Just out of curiosity, what language is that? Something Nordic?
Just tried the corrected code, still displaying the error message all the time. Including when I don't select anything to upload. I must say, your code looks fine to me? Then again, I also thought my code would do the trick! The language you see is 'Dutch', I'm from Holland! Hope you have a solution, thanks.
@user1555076 I would check each individual logic comparison and see which one is returning an improper true/false value. My regular expressions may not be 100% accurate - I tested them briefly before putting them in the answer, but not extensively. BTW I'm 1/8 Dutch (American). Nice to meet a true Hollander!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.