cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Go to solution
Highlighted

Unable to upload image in php oop

 I have class of banner in my files 

 

class Banner extends DatabaseObject {

static protected $table_name = 'vendor_banner';
static protected $db_columns = ['id','banner_name','vendor_id','banner_image'];


public $id;
public $banner_name;
public $vendor_id;
public $banner_image;
public $tmp_path;
public $upload_directory = "data/cataloguebanner";
public $custom_errors = array();
public $upload_errors_array = array(


UPLOAD_ERR_OK => "There is no error",
UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded.",
UPLOAD_ERR_NO_FILE => "No file was uploaded.",
UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder.",
UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk.",
UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload."


);

public function __construct($args=[]){
$this->banner_name = $args['banner_name'] ?? '';
$this->vendor_id = $args['vendor_id'] ?? '';
$this->banner_image = $args['banner_image'] ?? '';
}


public function set_file($file) {

if(empty($file) || !$file || !is_array($file)){

$this->errors[] = "There was no file uploaded here";
return false;

}
elseif($file['error'] !=0) {
$this->errors[] = $this->upload_errors_array[$file['error']];
return false;
}
else {
$ext = pathinfo($file['name'] , PATHINFO_EXTENSION);
$rand=rand(1111,9999);
$this->banner_image = $this->banner_name."-".rand().".".$ext;
$this->tmp_path = $file['tmp_name'];

}
}


public function picture_path() {

return $this->upload_directory.DS.$this->banner_image;
}

public function save_photo() {

if($this->id){
$this->update();
}
else{

if(!empty($this->errors)){
return false;
}

if(empty($this->banner_image) || empty($this->tmp_path)){
$this->errors[] = "The File was not available";
return false;
}

if(is_dir($this->upload_directory)==false){
mkdir($this->upload_directory, 0700); // Create directory if it does not exist
}
$target_path = SITE_ROOT . DS . 'vender' .DS. $this->upload_directory .DS. $this->banner_image;

if(file_exists($target_path)) {
$this->errors[] = "The file {$this->banner_image} already exists";
return false;
}
/// But it getting error here failed to open stream: No such file or directory in 
if(move_uploaded_file($this->tmp_path, $target_path)){
if($this->create()){
unset($this->tmp_path);
return true;
}
else {
$this->errors[] = "The file directory probably does not have permission";
return false;
}
}
}
}
}}

?>

1 REPLY 1
Super User I
Super User I
Solution

Re: Unable to upload image in php oop

Hello @Satyajeet17

Did you by chance increase your max filesize? Saw error code, "The uploaded file exceeds the upload_max_filesize directive in php.ini","

Very Respectfully,

Drew Davis
Navy Veteran and Entrepreneur | GoDaddy Pro user | "Proud to be serving others!"

*** Please note that I offer free advice on this forum. Please feel free to give me KUDOS on this topic/discussion; mark my comment as ACCEPTED SOLUTION if you believe I've helped solved your issue. Thanks! ***