coupe-r Posted December 16, 2011 Share Posted December 16, 2011 Hi All, I'm trying to validate file types and keep seeing an error. I only allow .gif, .jpg or .png. However, if I upload any of those file types, I get an error message.... If I echo out $filetypeCheck, I get image/png, which is corrent... $filetypeCheck = $_FILES["file"]["type"]; if( ($filetypeCheck != "image/gif") || ($filetypeCheck != "image/jpeg") || ($filetypeCheck != "image/png") ) { $val_error[] = 'File Type Error! (.gif, .jpg and .png only)'; } Whats going on here? Link to comment https://forums.phpfreaks.com/topic/253280-help-validating-uploaded-files/ Share on other sites More sharing options...
Psycho Posted December 16, 2011 Share Posted December 16, 2011 The problem is with your condition statement if( ($filetypeCheck != "image/gif") || ($filetypeCheck != "image/jpeg") || ($filetypeCheck != "image/png") ) NOTHING can pass that validation. If the image type was 'image/gif' then is does not equal 'image/jpeg' or 'image/png'. The problem is the OR statements and the negative checks. An image type will always not equal at least two of those consitions and validation fails. There are different solutions, such as if( !($filetypeCheck == "image/gif" || $filetypeCheck == "image/jpeg" || $filetypeCheck == "image/png") ) { //Invalid image type } else { //Valid image type } if( $filetypeCheck == "image/gif" || $filetypeCheck == "image/jpeg" || $filetypeCheck == "image/png") { //Valid image type } else { //Invalid image type } Or my favorite $validImageTypes = array('image/gif', 'image/jpeg', 'image/png'); if( !in_array($filetypeCheck, $validImageTypes)) { //Valid image type } else { //Invalid image type } Link to comment https://forums.phpfreaks.com/topic/253280-help-validating-uploaded-files/#findComment-1298412 Share on other sites More sharing options...
coupe-r Posted December 16, 2011 Author Share Posted December 16, 2011 That is now my favorite too. Works great man. Thanks!! Link to comment https://forums.phpfreaks.com/topic/253280-help-validating-uploaded-files/#findComment-1298416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.