它应该返回true时返回false?

时间:2012-07-07 16:49:31

标签: php class methods return

我有一种方法可以检查缩略图是否有效上传。由于某种原因,它在调用程序中返回false。

图像文件绝对符合我为其设置的正确尺寸,尺寸,文件类型的要求,并且文件中没有错误。

这是图像文件的print_r()

imageArray ( [file] => Array ( [name] => juliensbook2slide.jpg [type] => image/jpeg [tmp_name] => C:\Users\chris\AppData\Local\Temp\php5A99.tmp [error] => 0 [size] => 20590 ) )

以下是方法代码:

public function checkThumb(){
        $this->temp_dir = $_FILES['file']['tmp_name'];
        $this->image_type = $_FILES['file']['type'];
        $this->image_size = $_FILES['file']['size'];
        $this->image_name = $_FILES['file']['name'];
        $this->image_error = $_FILES['file']['error'];
        @$this->image_dimensions = getimagesize($this->temp_dir);
        $this->image_width = $this->image_dimensions[0]; // Image width
        $this->image_height = $this->image_dimensions[1]; // Image height
        $this->path = '';
        $this->error = '';
        if(!empty($this->image_name) && $this->image_error == 0){

            $this->path = 'img/thumb_/'.$this->random_name. $_FILES['file']['name'];

            if(in_array($this->image_type, $this->allowed_type)){

                if($this->image_size <= $this->max_size){

                    if(($this->image_width < $this->max_width) && $this->image_height < $this->max_height){


                        return true;

                    }else{
                        return $error = 'ERROR: Image dimension must be no larger than 4050x4050';
                    }

                }else{
                    return $error = 'ERROR: Image size must be no larger than 5MB';
                }
            }else{
                return $error = 'ERROR: image must be .jpg, .gif, .png only.';
            }
        }else {
            return false;
        }
    }

以下是不移动上传图片的代码,因为它返回false:

if($register->checkThumb){
                //send image to permanent image directory
                $register->moveUploadedImage();

                //if the thumbnail failed validation put the error message in variable
            }else if(is_string($register->checkThumb())){
                $message = $register->checkThumb();

            }
                print_r($_FILES);
                //put record of user into database
                $register->convertSex();
                $register->insertIntoDB($thumb);
}

那么为什么它会返回假?

1 个答案:

答案 0 :(得分:2)

你没有打电话给这个方法。方法名称后面没有圆括号。所以基本上你要检查是否设置了名为checkThumb的属性。

$register->checkThumb$register->checkThumb()相同。

这应该有效:

if($register->checkThumb()){  //send image to permanent image directory
    $register->moveUploadedImage();
}  //if the thumbnail failed validation put the error message in variable
else if(is_string($register->checkThumb())) {
    $message = $register->checkThumb();
}

但我建议不要调用相同的方法3次,所以我会使用以下内容:

$checked_thumb = $register->checkThumb();
if($checked_thumb){  //send image to permanent image directory
    $register->moveUploadedImage();
}  //if the thumbnail failed validation put the error message in variable
else if(is_string($checked_thumb)) {
    $message = $checked_thumb;
}