Reputation: 10376
I have a validation function I'm using inside of codeigniter.
function valid_image() {
if ( ($_FILES["file"]["type"] != "image/jpeg") || ($_FILES["file"]["type"] != "image/gif") ) {
$this->form_validation->set_message('valid_image', 'Wrong file type..');
return false;
} else {
return true;
}
With just the "image/jpeg" part in the if statement it works fine. If I try to upload anything other than a jpg file it fails. If I run the code above, it fails with both a jpg or a gif file.
And before someone says "why not use the upload class," I can't. I'm saving my pics directly into MongoDB, so the upload class doesn't help much.
Upvotes: 0
Views: 276
Reputation: 88806
This doesn't really answer your question, but...
The $_FILE[blah]["type"]
parameter is set by the web browser and as such is user data that can't be trusted.
You may want to use exif_imagetype($_FILES["file"]["tmp_name"])
instead to detect the true image type.
function valid_image() {
$type = exif_imagetype($_FILES["file"]["tmp_name"]);
if (($type != IMAGETYPE_GIF) && ($type != IMAGETYPE_JPEG)) {
$this->form_validation->set_message('valid_image', 'Wrong file type..');
return false;
} else {
return true;
}
}
Edit: If the exif extension isn't installed, you can also do this:
$sizes = getimagesize($_FILES["file"]["tmp_name"]);
$sizes[2]
will contain a value corresponding to one of the IMAGETYPE constants.
function valid_image() {
$sizes = getimagesize($_FILES["file"]["tmp_name"]);
if (($sizes[2] != IMAGETYPE_GIF) && ($sizes[2] != IMAGETYPE_JPEG)) {
$this->form_validation->set_message('valid_image', 'Wrong file type..');
return false;
} else {
return true;
}
}
Upvotes: 1
Reputation: 4210
Well if you try that statement with a .gif, it's going to say it's the wrong file because it checks the first 'or' first. Also, if you try and run a .jpg, it'll pass the first or but then fail the second, therefore it will still say it's invalid. Try making it '&&' instead of '||', that way it'll check and make sure it's one of the two every time.
Upvotes: 0
Reputation: 42360
you want to use an and, not an or. In your above code, jpg fails because it's not a gif, and gif fails because it's not a jpg.
function valid_image() {
if ( ($_FILES["file"]["type"] != "image/jpeg") && ($_FILES["file"]["type"] != "image/gif") ) {
$this->form_validation->set_message('valid_image', 'Wrong file type..');
return false;
} else {
return true;
}
Upvotes: 0
Reputation: 1
The statement as you have it guarantees that it is not jpeg and not gif. The problem with that is that if it is jpeg, it is still not gif, so it returns false. In fact, it always returns false since something can not be two things at once.
Change the || to && if that is your intent and it should help.
Upvotes: 0
Reputation: 6660
You need AND instead of OR
if (($_FILES["file"]["type"] != "image/jpeg") &&
($_FILES["file"]["type"] != "image/gif"))
Upvotes: 1
Reputation: 42140
you need your condition to be
if ( ( $_FILES["file"]["type"] != "image/jpeg")
&& ($_FILES["file"]["type"] != "image/gif") ) {...}
if it's not a jpeg AND it's not a gif - it's not valid
Upvotes: 1
Reputation: 54030
Your if statement wrong. You should use '&&' instead of '||' (DeMorgan's law).
Upvotes: 1
Reputation: 10482
If the file is jpg, then it's not gif, and you get the message.
If the file is gif, then it's not jpg, and you still get the message.
You have "file is not jpg OR file is not gif". Replace || with && and you will get the message only when "file is not jpg AND file is not gif".
Upvotes: 5