I have a method that checks if a thumbnail is validate for uploading. For some reason it is returning false in the calling program.
The image file definitely meets the requirements that I set for it in terms of correct size, dimensions, file type and there are no errors in the file.
This is the print_r() of the Image file:
imageArray ( [file] => Array ( [name] => juliensbook2slide.jpg [type] => image/jpeg [tmp_name] => C:\Users\chris\AppData\Local\Temp\php5A99.tmp [error] => 0 [size] => 20590 ) )
Here is the method code:
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;
}
}
and here is the code where it is not moving the uploaded image because it is returning 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);
}
So why is it returning false?
You weren’t calling the method. There were no round brackets after the method name. So basically you were checking if a property named checkThumb is set.
As in
$register->checkThumbvs.$register->checkThumb().This should work:
But i would recommend not calling the same method 3 times so I’d use the following: