bugzy Posted July 16, 2012 Share Posted July 16, 2012 I'm validating if a user chose an image file or not but it seemed like it's not working.. I have this form: <form method="post" name="add_item" enctype="multipart/form-data" action="add_item.php"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <input name="uploaded_image" type="file" /> <input type="submit" value="Add Item" name="submit" size="30" /> </form> php code: <?php if(isset($_FILES['uploaded_image'])) { //User has chose a file } else { //Uder didn't choose a file } ?> It keeps on telling me that I have chose a file even if I didn't. I also tried using "empty" but it didn't work... anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265726-validation-on-image-file-type-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2012 Share Posted July 16, 2012 Add this right after the opening php tag and I'm sure you'll see why that's happening. echo '$_FILES array:<pre>'; print_r($_FILES); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/265726-validation-on-image-file-type-not-working/#findComment-1361769 Share on other sites More sharing options...
bugzy Posted July 16, 2012 Author Share Posted July 16, 2012 Add this right after the opening php tag and I'm sure you'll see why that's happening. echo '$_FILES array:<pre>'; print_r($_FILES); echo '</pre>'; If I'm not choosing a file this is what I'm getting <?php $_FILES array: Array ( [uploaded_image] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) ) ?> if I chose a file here's what I'm getting <?php $_FILES array: Array ( [uploaded_image] => Array ( [name] => matzhee_logo.gif [type] => image/gif [tmp_name] => C:\wamp\tmp\php73B9.tmp [error] => 0 [size] => 1189 ) ) ?> Quote Link to comment https://forums.phpfreaks.com/topic/265726-validation-on-image-file-type-not-working/#findComment-1361770 Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2012 Share Posted July 16, 2012 Exactly. Do you see why checking to see if $_FILES['upoaded_image'] isset() isn't going to work? Quote Link to comment https://forums.phpfreaks.com/topic/265726-validation-on-image-file-type-not-working/#findComment-1361771 Share on other sites More sharing options...
bugzy Posted July 16, 2012 Author Share Posted July 16, 2012 Exactly. Do you see why checking to see if $_FILES['upoaded_image'] isset() isn't going to work? Here you go <?php if($_FILES['uploaded_image']['error'] == 4) { //User didn't choose a file } else { //User choose a file } ?> Thanks Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/265726-validation-on-image-file-type-not-working/#findComment-1361772 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.