When I want to update record in a database table which have images uploading it can't upload the image but all the other data updated successfully Here is the update function:
$config['upload_path'] = './img/products/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/admin_header_view');
$this->load->view("admin/admin_menu_view");
$this->load->view('admin/admin_rewarding_view', $error);
$this->load->view('admin/admin_footer_view');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename = $data['upload_data']['file_name'];
$data= array(
'reward' => $this->input->post('reward'),
'amount_of_points' => $this->input->post('amount_of_points'),
'img' => $filename
);
$this->db->where('id',$this->input->post('id'));
$this->db->update('rewards',$data);
redirect("admin/rewarding_system");
}
if ( ! $this->upload->do_upload($this->input->post('upload_name_attr')) //or however you are getting this file to the server
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/admin_header_view');
$this->load->view("admin/admin_menu_view");
$this->load->view('admin/admin_rewarding_view', $error);
$this->load->view('admin/admin_footer_view');
}
Use This. I am giving my example. According to requirement, do the needful change. This is the way,i use my file uploading. It will help you also. just go through it. Lot of junk codes are there, neglect all. Use what u want from here. Hope. I Helped You.
<?php echo form_open_multipart('welcome/do_upload'); ?>
<input type='text' class='form-control' name='FileTitle'> <br>
<textarea class='form-control' name='FileDesc' rows='4'></textarea> <br>
<input type='file' class='form-control' name='ChangedFileName'>
</form>
class Welcome extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->library('session'); // Start Session
$this->load->helper('form');
$this->load->library('form_validation');
}
function do_upload()
{
$FileDescription = $this->input->post('FileDesc');
$FileID = $this->input->post('FileID');
$FileTitle = $this->input->post('FileTitle');
$UploadDirectory='assets/Upload/';
$FileName = $_FILES['ChangedFileName']['name'];
$EncFileName=time().$FileName;
if($FileName!='')
{
if(move_uploaded_file($_FILES['ChangedFileName']['tmp_name'], $UploadDirectory.$EncFileName))
{
$FileUpload=$this->news_model->UploadEditedFiles($EncFileName,$FileID,$FileTitle,$FileDescription);
if($FileUpload=='True')
{
redirect('welcome/editrecord/'.$FileID);
}
}
}
}
}
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function UploadEditedFiles($EncFileName,$FileID,$FileTitle,$FileDescription)
{
$data = array('FilePath' => $EncFileName,'FileID'=>$FileID);
$this->db->where('FileID', $FileID);
$this->db->insert('MemberFiles', $data);
}
}
Check with your Initialization
:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->upload->initialize($config);
$this->load->library('upload', $config);