sk121506 Posted January 20, 2008 Share Posted January 20, 2008 Every time i try and move code around i always end up getting the error check message somehow with the file type (the image). It will even upload correctly and still give me the invalid file type message. Tried so many methods and just keep getting frustrated. Any help would be appreciated. <form action="<?php echo $_SERVER['php_SELF'];?>" method="post" enctype="multipart/form-data"> <table class="main" cellpadding="0" cellspacing="0" border="0"> <tr> <td><font color="#002157"><b>Upload Image:</b></font>  </td> <td><input type="file" size="40" name="file" id="file"></td> </tr> <tr> <td align="right"><font color="#002157"><b>Image URL:</b></font>  </td> <td><input type="text" size="40" name="URL"></td> </tr> <tr> <td> </td> <td><br><input type="submit" name="submit" value="Submit"></td> </tr> </table> </form> <?php list($width, $height) = getimagesize($_FILES['file']['tmp_name']); if($_FILES["file"]["name"]!=""){ if ($_FILES["file"]["type"] != "image/gif" || $_FILES["file"]["type"] != "image/jpeg" || $_FILES["file"]["type"] != "image/pjpeg" || $_FILES["file"]["type"] != "image/png" || $_FILES["file"]["type"] != "image/x-png"){ echo "Invalid file type.<br>"; } if ($_FILES["file"]["error"] > 0){ echo "Error: " . $_FILES["file"]["error"] . "<br>"; } if($width!=300 || $height!=200){ echo "Bad dimensions.<br>"; } if($_FILES["file"]["size"] > 100000){ echo "File is over the 100KB limit.<br>"; } else{ echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"]/1024) . "KB<br>"; //QUERY insert into database } } else{ echo ""; } ?> EDITED BY thorpe: Added tags to enable syntax highlighting. Quote Link to comment https://forums.phpfreaks.com/topic/86860-solved-uploaded-image-still-says-invalid/ Share on other sites More sharing options...
Stooney Posted January 20, 2008 Share Posted January 20, 2008 Well according to your code, if file type does not equal all of the file types, it will display an error. Try something like putting the allowed file types into an array (let's call it $allowed), and using if(!in_array($_FILES['file']['type'], $allowed)){ //error } Quote Link to comment https://forums.phpfreaks.com/topic/86860-solved-uploaded-image-still-says-invalid/#findComment-444005 Share on other sites More sharing options...
taith Posted January 20, 2008 Share Posted January 20, 2008 you'd need to use this http://ca3.php.net/function.move-uploaded-file to move the file to a valid location, else it stays in your temp folders and gets deleted... Quote Link to comment https://forums.phpfreaks.com/topic/86860-solved-uploaded-image-still-says-invalid/#findComment-444007 Share on other sites More sharing options...
sk121506 Posted January 20, 2008 Author Share Posted January 20, 2008 i changed some things around to what you both said... yet... what happens now is the invalid type does not show when a successful upload occurs (GOOD!) and each error message shows up respectively (GOOD!) except after a changed some things around to a better implementation (which shows below) what happens now is... when the correct image is supposed to be uploaded, no success message appears. i want to test each error message and if the image passes, move on to success code. if there is another way around it without putting in each check field, please let me know! if someone sees something totally wrong... PLEASE help!! <?php list($width, $height) = getimagesize($_FILES['file']['tmp_name']); $allowed = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png"); if($_FILES["file"]["name"]!=""){ if (!in_array($_FILES["file"]["type"], $allowed)){ echo "Invalid file type.<br>"; } if ($_FILES["file"]["error"] > 0){ echo "Error: " . $_FILES["file"]["error"] . "<br>"; } if($width!=300 || $height!=200){ echo "Bad dimensions.<br>"; } if($_FILES["file"]["size"] > 100000){ echo "File is over the 100KB limit.<br>"; } if(in_array($_FILES["file"]["type"], $allowed) && $_FILES["file"]["error"] < 0 && $width=300 && $height=200 && $_FILES["file"]["size"] < 100000){ move_uploaded_file($_FILES["file"]["tmp_name"], "" . $_FILES["file"]["name"]); echo "Stored in: " . "" . $_FILES["file"]["name"]; echo "<br>Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"]/1024) . "KB<br>"; //QUERY insert into database } } else{ echo ""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86860-solved-uploaded-image-still-says-invalid/#findComment-444065 Share on other sites More sharing options...
sk121506 Posted January 20, 2008 Author Share Posted January 20, 2008 just to edit my remarks above, i had to add elseif statements to the error check messages Quote Link to comment https://forums.phpfreaks.com/topic/86860-solved-uploaded-image-still-says-invalid/#findComment-444085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.