如果/其他不正常 [英] If/else isn't working properly
问题描述
我在codeigniter内部使用了一个验证功能。
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;
}
在if语句中仅使用 image / jpeg部分就可以了。如果我尝试上传除jpg文件以外的任何内容,它将失败。如果我运行上面的代码,则它会同时显示jpg或gif文件。
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.
在有人说为什么不使用上载类之前,我不能。我将照片直接保存到MongoDB中,因此上载类没有太大帮助。
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.
推荐答案
回答您的问题,但是...
This doesn't really answer your question, but...
已设置 $ _ FILE [blah] [ type]
参数
您可能想使用 exif_imagetype($ _ FILES [ file
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;
}
}
编辑:如果未安装exif扩展,您还可以执行以下操作:
If the exif extension isn't installed, you can also do this:
$sizes = getimagesize($_FILES["file"]["tmp_name"]);
$ sizes [2]
将包含一个与 IMAGETYPE常量之一相对应的值。
$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;
}
}
这篇关于如果/其他不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!