Vijaykarthik Vijaykarthik - 1 year ago 67
PHP Question

How to upload files in codeigniter

I am using codeigniter framework for my projec, I am uploading images, so i have use some code. other data was inserted but image was not inserted, i have attached my code below
Thank you in advance

Html:

<form method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>charity/insertCharity">
<div class="control-group" style="padding:15px;">
<label class="control-label" for="basicinput" style="padding:10px;">Logo:</label>
<div class="controls">
<div class="input-append span6">
<input type="file" class="span12" placeholder="Upload file" name="logo">
</div>
</div>
</div>




Controller:

public function insertCharity(){
$result = $this->charity_model->addCharity();
if($result > 0){
$data['mess'] = "Charity added successfully";
$this->load->view('charity_view', $data);
}
}


Model:

public function addCharity(){
$cha_name = $this->input->post('charity_name');
$regno = $this->input->post('reg_no');
$contact_per = $this->input->post('contact_person');
$contact_no = $this->input->post('contact_no');
$address = $this->input->post('address');
$aboutCharity = $this->input->post('about_charity');
$createdOn = date('Y-m-d H:i:s');
if($_FILES['logo']['size'] != 0 )
{

$files = $_FILES;
$config = array();
$config['upload_path'] = "charity_gallery/";
$config['allowed_types'] = "png|jpg|gif";
$config['max_size'] = "5000";
$this->load->library('upload',$config);
$this->upload->initialize($config);


$this->upload->do_upload('logo');
$imgdata = $this->upload->data();
$image_config=array();
$image_config["image_library"] = "gd2";
$image_config['overwrite'] = TRUE;
$image_config["source_image"] = $imgdata["full_path"];
$image_config['create_thumb'] = FALSE;
$image_config['maintain_ratio'] = TRUE;
$image_config['new_image'] = $imgdata["file_path"].$imgdata["file_name"];
$image_config['quality'] = "95%";
$image_config['width'] = 170;
$image_config['height'] = 170;
$this->load->library('image_lib',$image_config);
$this->image_lib->initialize($image_config);
$this->image_lib->resize();
$logo = 'charity_gallery/'.$imgdata["file_name"];




$query = $this->db->query("INSERT INTO `charities` (charity_name, reg_no, contact_person, contact_number, address, about, logo, created_on) VALUES ('$cha_name', '$regno', '$contact_per', '$contact_no', '$address', '$aboutCharity', '$logo', '$createdOn')");
//$cid = $this->db->insert_id();
$aff = $this->db->affected_rows();
return $aff;
}else{
$query = $this->db->query("INSERT INTO `charities` (charity_name, reg_no, contact_person, contact_number, address, about, created_on) VALUES ('$cha_name', '$regno', '$contact_per', '$contact_no', '$address', '$aboutCharity', '$createdOn')");
//$cid = $this->db->insert_id();
$aff = $this->db->affected_rows();
return $aff;
}


}


All value is getting inserted, but logo alone not getting insert folder name alone inserted like "charity_gallery". I dont know where i am going wrong, please guide me

Answer Source

Actually i was using ubuntu machine so i forgot to gave permission for the image folder. After i gave permission i got answer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download