0

我有一种方法可以检查缩略图是否可以上传。由于某种原因,它在调用程序中返回 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;
        }
    }

这是它没有移动上传的图像的代码,因为它返回错误:

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);
}

那么为什么它返回错误呢?

4

1 回答 1

2

您没有调用该方法。方法名称后没有圆括号。所以基本上你正在检查是否设置了名为 checkThumb 的属性。

就像在$register->checkThumbvs. $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;
}
于 2012-07-07T16:51:55.483 回答