fife Posted July 6, 2011 Share Posted July 6, 2011 Ok i have this script that uploads an image. Works good thanks to all you guys out there. There are issues though. I have tried everything but none of the error checking is working. I check for 3 things. 1. file size is less than 2 MB 2. file exists 3. file is a certain type. None of these checkers seems to be working properly. if the image exceeds the file size no error is displayed but the image does not upload. if you try to upload nothing the whole script runs and then crashes my hosts server!!!!!!!!!! (Not good) if you try to upload the none file types allows again it crashes my hosts server!!!!!!!!!!! Basically Im not good enough at php YET to find what my error in the code is as if you upload a correct type and correct size it works. Please please please help! Thank you if (isset($_POST['Save_photo'])) { //another photo uploader $fileName1 = trim($_FILES['new_photo']['name']); $tmpName = $_FILES['new_photo']['tmp_name']; $fileSize = $_FILES['new_photo']['size']; $fileType = $_FILES['new_photo']['type']; $randName = md5(rand() * time()); $fileName = $randName.$fileName1; $limit_size='2097152'; $folder = "{$_SERVER['DOCUMENT_ROOT']}members/images/{$row_Club['county']}/"; if ($fileSize >= $limit_size){$msg1="Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>"; $success=0;} // } elseif ($fileName1=="") {$error = "Please choose a file to upload"; $success=0;} //elseif ($fileType!== 'image/jpeg' || 'image/gif' || 'image/png' || 'image/JPG') {$error = "false";} $types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png'); if (in_array($fileType, $types)) { // Your file handing script here if(move_uploaded_file($tmpName , $folder.$fileName)) { $Fnew = $fileName; $county = $User['county']; $time = date("h:i A, d/m/Y"); $qInsert = mysql_query("INSERT INTO `images` (`image_name`, `creation_date`, `user`, `county`, club_ID) VALUES ('$Fnew', '$time', '".$User['memberDID']."', '$county', '".$row_Club['clubID']."')") or die (mysql_error()); $image = mysql_insert_id(); $url = "New-photo-details.php?image=$image"; header("Location: $url"); } } else { $message = "This file does not have a valid extension! Only JPG, PNG, GIF are allowed."; $success = 0; } } Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/ Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 first, on this line $limit_size='2097152'; by wrapping the number in quotes, you are treating it as a string, so therefore it will always pass your if statement regardless, remove the quotes $limit_size = 2097152; As far as your check for an empty name, if the user tries to upload a blank image, your code will simply store an error into a variable, not even echo the error, and wont hault the script which it should. elseif ($fileName1=="") {print "Please choose a file to upload"; $success=0; exit("failure!");} For number 3, the in_array() function should work to check for a valid file extension...I will need to look further at it Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/#findComment-1239179 Share on other sites More sharing options...
fife Posted July 6, 2011 Author Share Posted July 6, 2011 cool thank you ak. Ill be back on in 30 mins to try your changes as my server has crashed. I will post what happens. Again thank you Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/#findComment-1239182 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 No, PHP will act all smart and covert it to an integer before comparting. <?php $num = '15'; if( $num > 10 ) echo $num.' is > 10<br>'; if( $num < 20 ) echo $num.' is < 20<br>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/#findComment-1239183 Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 No, PHP will act all smart and covert it to an integer before comparting. <?php $num = '15'; if( $num > 10 ) echo $num.' is > 10<br>'; if( $num < 20 ) echo $num.' is < 20<br>'; ?> yes you're right man, my fault Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/#findComment-1239214 Share on other sites More sharing options...
fife Posted July 7, 2011 Author Share Posted July 7, 2011 ok yesterday I posted that none of my error checking was working and that I kept crashing the server. Thanks to some of the posts above two of the error checks works. How ever. The major one that crashed my server yesterday still does not. Im trying to restrict file sizes to only allow below 2MB When I try to upload one the issue is it runs the whole upload (very slow). Then refreshes the page without actually uploading the file. Obvisouly its good the file does not upload but its not good that it tries. It should fail long before the move_upload_file. Here is the code if (isset($_POST['Save_photo'])) { //another photo uploader $fileName1 = trim($_FILES['new_photo']['name']); $tmpName = $_FILES['new_photo']['tmp_name']; $fileSize = $_FILES['new_photo']['size']; $fileType = $_FILES['new_photo']['type']; $randName = md5(rand() * time()); $fileName = $randName.$fileName1; $limit_size = 2097152; $folder = "{$_SERVER['DOCUMENT_ROOT']}members/images/{$row_Club['county']}/"; //now the error check that does not seem to run if ($fileSize >= $limit_size){ print "<div id='clear'></div><p class='formerrors'>Your uploaded file size is more than 2MB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.</p><div id='clear'>"; exit();} $types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png'); if (in_array($fileType, $types)) { move_uploaded_file($tmpName , $folder.$fileName); } else { $type = "This file does not have a valid extension! Only JPG, PNG, GIF are allowed."; } } Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/#findComment-1239507 Share on other sites More sharing options...
fife Posted July 7, 2011 Author Share Posted July 7, 2011 ok ive found something but I under understand it I changed my code to if (isset($_POST['Save_photo'])) { $fileName1 = trim($_FILES['new_photo']['name']); $tmpName = $_FILES['new_photo']['tmp_name']; $fileSize = $_FILES['new_photo']['size']; $fileType = $_FILES['new_photo']['type']; $randName = md5(rand() * time()); $fileName = $randName.$fileName1; print $fileSize; exit(); } for testing reasons. If I upload a small file say 270KB it echos 276475 If i then upload a file size 20778KB it echos 0 Can anybody explain this please? Thank you Danny Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/#findComment-1239517 Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 the default upload size is 2MB...20778KB is much larger than 2MB, so the file wont even upload.. You can change these setting in your php.ini file by looking for upload_max_filesize and post_max_size Quote Link to comment https://forums.phpfreaks.com/topic/241243-error-checking-on-image-upload-failing/#findComment-1239566 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.