bugzy Posted July 16, 2012 Share Posted July 16, 2012 Can't get validation to work <?php if ((($_FILES["uploaded_image"]["type"] != "image/gif") || ($_FILES["uploaded_image"]["type"] != "image/jpeg") || ($_FILES["uploaded_image"]["type"] != "image/png")) && ($_FILES["uploaded_image"]["error"] != 4)) { //not valid type } else { //is valid type } ?> even if I'm choosing jpeg, png and gif it keeps on telling me that it is an invalid file. I tried to print_r each files that I'm uploading and I'm pretty sure each file has the same value type as the conditions above.. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265731-_filetype-validation-issue/ Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2012 Share Posted July 16, 2012 Because you're using negative comparisons, you need to use &&, not || for those comparisons. Using the || operator, the conditional will evaluate to TRUE as soon as any of the conditions are met. You want it to evaluate to TRUE only if ALL of the conditions are met. IOW if the file is not a jpg, and the file is not a gif, and the file is not a png, then it isn't a valid type. You should check the error field separately, and a better way to do that is to check if it's a zero. If it's anything other than a zero, there was an error. Quote Link to comment https://forums.phpfreaks.com/topic/265731-_filetype-validation-issue/#findComment-1361787 Share on other sites More sharing options...
Donald. Posted July 16, 2012 Share Posted July 16, 2012 The problem, as I see it, is that the file will always either not be a jpeg or png or a gif. Since when its a png it will not be a gif or jpeg. I was dealing with something similar yesterday. I think this should do the trick. <?php if (!($_FILES["uploaded_image"]["type"] == "image/gif" || $_FILES["uploaded_image"]["type"] == "image/jpeg" || $_FILES["uploaded_image"]["type"] == "image/png") && $_FILES["uploaded_image"]["error"] != "4") { //not valid type } else { //is valid type } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265731-_filetype-validation-issue/#findComment-1361788 Share on other sites More sharing options...
bugzy Posted July 16, 2012 Author Share Posted July 16, 2012 Because you're using negative comparisons, you need to use &&, not || for those comparisons. Using the || operator, the conditional will evaluate to TRUE as soon as any of the conditions are met. You want it to evaluate to TRUE only if ALL of the conditions are met. IOW if the file is not a jpg, and the file is not a gif, and the file is not a png, then it isn't a valid type. You should check the error field separately, and a better way to do that is to check if it's a zero. If it's anything other than a zero, there was an error. Thanks again Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/265731-_filetype-validation-issue/#findComment-1361789 Share on other sites More sharing options...
bugzy Posted July 16, 2012 Author Share Posted July 16, 2012 The problem, as I see it, is that the file will always either not be a jpeg or png or a gif. Since when its a png it will not be a gif or jpeg. I was dealing with something similar yesterday. I think this should do the trick. <?php if (!($_FILES["uploaded_image"]["type"] == "image/gif" || $_FILES["uploaded_image"]["type"] == "image/jpeg" || $_FILES["uploaded_image"]["type"] == "image/png") && $_FILES["uploaded_image"]["error"] != "4") { //not valid type } else { //is valid type } ?> GOAT 1st post I tried it and it works Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/265731-_filetype-validation-issue/#findComment-1361790 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.