Half the time when I upload the same image (only if it's above 2M😎 will I get an error saying it's not a jpg or a gif. Of course it is but it's not working 100% of the time. Why would this happen and how can you fix it? Also how would you check to make sure the file you are uploading isn't already there or written over?

upload1.html

<form name="upload" action="upload4.php" method="POST" ENCTYPE="multipart/form-data">
Select the file to upload: <input type="file" name="userfile">
<input type="submit" name="upload" value="upload">
</form>

upload4.php

<?php
$imageinfo = getimagesize($_FILES['userfile']['tmp_name']);

$blacklist = array(".php", ".phtml", ".htm", ".zip", ".exe", ".asp", ".aspx", ".xml");

foreach ($blacklist as $item) {
   if(preg_match("/$item\$/i", $_FILES['userfile']['name'])) {
       echo "We do not allow uploading PHP files or executable scripts\n";
       exit;
   }
}

if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {
    echo "Sorry, we only accept GIF and JPEG images\n";
    exit;
}

$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

		if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
		    echo "File is valid, and was successfully uploaded.\n";
		} else {
		    echo "File uploading failed.\n";
		}
?>

    What is the exact error printed? Is it: "Sorry, we only accept GIF and JPEG images" ?

      Correct, the error I get is:

      "Sorry, we only accept GIF and JPEG images"

        No sure where your getting the mime from but I believe it should be type:
        $_FILES['userfile']['type'], see. http://us2.php.net/features.file-upload. Also, don't use and, because it could be one OR the other that should cause it to fail.

        if ($imageinfo['type'] != 'image/gif' || $imageinfo['type'] != 'image/jpeg') {
            echo "Sorry, we only accept GIF and JPEG images\n";
            exit;
        }
        

          Ah, sorry just saw the getimagesize(), overlooked it the first time. Anyway, try changing the && to ||.

            virtualdevl;10885679 wrote:

            No sure where your getting the mime from but I believe it should be type:
            $_FILES['userfile']['type'], see. http://us2.php.net/features.file-upload. Also, don't use and, because it could be one OR the other that should cause it to fail.

            if ($imageinfo['type'] != 'image/gif' || $imageinfo['type'] != 'image/jpeg') {
                echo "Sorry, we only accept GIF and JPEG images\n";
                exit;
            }
            

            I changed it to type and it seems to be working but when I change it to or (||) from and (&&) it fails every time. I believe that is because it reads (in english). If the file is not gif and if the file is not jpeg then die.

              Write a Reply...