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";
}
?>