File uploading in CodeIgniter has two main parts. The frontend and the backend. The frontend is handled by the HTML form that uses the form input type file. On the backend, the file upload library processes the submitted input from the form and writes it to the upload directory.
(toc) #title=(Table of Content)
CONTROLLER
public function banner()
{
$data['title'] = 'banner';
$data['banner'] = $this->setting_model->readBanner();
$this->form_validation->set_rules('name', "Name", "required");
// $this->form_validation->set_rules('video', "Banner Images", "required");
if (empty($_FILES['banner_image']['name']))
{
$this->form_validation->set_rules('banner_image','Banner Images','required');
}
// ---------------------------UPLOAD IMAGE-----------------------------
$config = [
'upload_path' => 'upload/banner/',
'allowed_types' => 'gif|png|jpg|jpeg|PNG|JPG|JPEG',
'overwrite' => false,
'maintain_ratio' => true,
'encrypt_name' => true,
'remove_spaces' => true,
'file_ext_tolower' => true
];
$this->load->library('upload', $config);
if ($this->upload->do_upload('banner_image')) {
$data = $this->upload->data();
$image = $config['upload_path'].$data['file_name'];
}
else{
if(!empty($_FILES['banner_image']['name'])){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('exception',$this->upload->display_errors());
}
// redirect('customer/service/security');
}
// print_r($userdata);die();
// ---------------------------UPLOAD IMAGE END-----------------------------
$userdata = array(
'name' => $this->input->post('name'),
// 'banner_image' => $this->input->post('banner_image'),
'date' => date("Y-m-d h:i:s"),
);
// print_r($userdata);die();
$userdata['banner_image']=(!empty($image)?$image:$this->input->post('banner_image'));
if ($this->form_validation->run())
{
if ($this->setting_model->createBanner($userdata)) {
$this->session->set_flashdata('message', 'Video added successfully!');
redirect("backend/dashboard/setting/banner");
} else {
$this->session->set_flashdata('exception', display('please_try_again'));
redirect("backend/dashboard/setting/banner");
}
}
$data['content'] = $this->load->view("backend/dashboard/banner", $data, true);
$this->load->view("backend/layout/main_wrapper", $data);
}
public function delete_banner($banner_id = null)
{
if ($this->setting_model->deleteBanner($banner_id)) {
$this->session->set_flashdata('message',"Banner Image deleted successfully!");
} else {
$this->session->set_flashdata('exception', display('please_try_again'));
}
redirect("backend/dashboard/setting/banner");
}
// ----------------------- Banner End ---------------------------------//(code-box)
MODEL
//--------------------------Banner--------------------------------------//public function createBanner($data = array()){return $this->db->insert('banner', $data);//die($this->db->last_query());}public function readBanner(){return $this->db->select("*")->from('banner')->order_by('id', 'desc')// ->where('status','yes')->get()->result();}public function deleteBanner($banner_id = null){return $this->db->where('id', $banner_id)->delete("banner");}//--------------------------Banner End--------------------------------------//(code-box)
VIEW PAGE
<div class="app-content content">
<div class="content-wrapper">
<div class="row match-height">
<!-- my task Timeline -->
<div class="col-xl-12 col-lg-12">
<div class="card">
<div class="card-header border-0">
<h4 class="card-title"><?php echo (!empty($title)?$title:null) ?></h4>
<div class="heading-elements">
<ul class="list-inline">
<li><a data-action="reload"><i class="feather icon-rotate-cw"></i></a></li>
<li><a data-action="expand"><i class="feather icon-maximize"></i></a></li>
</ul>
</div>
</div>
<div class="card-content">
<div class="card-body">
<?php echo form_open_multipart("backend/dashboard/setting/banner") ?>
<div class="form-group row">
<label for="headline_en" class="col-sm-2 col-form-label">Name : <i class="text-danger">*</i></label>
<div class="col-sm-8">
<input name="name" value="" class="form-control" placeholder="Name" type="text" id="name">
</div>
</div>
<div class="form-group row">
<label for="headline_en" class="col-sm-2 col-form-label">Banner Image : <i class="text-danger">*</i></label>
<div class="col-sm-8">
<input name="banner_image" class="form-control" placeholder="banner image" type="file" id="banner_image">
</div>
</div>
<div class="row">
<div class="col-sm-9 col-sm-offset-2">
<button type="submit" class="btn btn-success w-md m-b-5">Upload</button>
</div>
</div>
<?php echo form_close() ?>
</div>
</div>
</div>
</div>
</div>
<div class="row match-height">
<!-- my task Timeline -->
<div class="col-xl-12 col-lg-12">
<div class="card">
<div class="card-header border-0">
<h4 class="card-title"><?php echo (!empty($title)?$title:null) ?> List</h4>
<div class="heading-elements">
<ul class="list-inline">
<li><a data-action="reload"><i class="feather icon-rotate-cw"></i></a></li>
<li><a data-action="expand"><i class="feather icon-maximize"></i></a></li>
</ul>
</div>
</div>
<div class="card-content">
<div class="card-body">
<div id="audience-list-scroll" class="table-responsive position-relative">
<table class="table">
<thead>
<tr>
<th><?php echo display('sl_no') ?></th>
<th>Name</th>
<th>Banner Images</th>
<th>Date</th>
<th><?php echo display('Action') ?></th>
</tr>
</thead>
<tbody>
<?php if (!empty($banner)) ?>
<?php $sl = 1; ?>
<?php foreach ($banner as $value) { ?>
<tr>
<td><?php echo $sl++; ?></td>
<td><?php echo $value->name; ?></td>
<td> <img src="<?php echo base_url().$value->banner_image ?>" width="150"></td>
<td><?php echo $value->date; ?></td>
<td>
<a href="<?php echo base_url("backend/dashboard/setting/delete_banner/$value->id") ?>" onclick="return confirm('<?php echo display("are_you_sure") ?>')" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="right" title="Delete "><i class="fa fa-trash-o" aria-hidden="true"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo $links; ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div> (code-box)
Create New Database
id int(10) AUTO_INCREMENT
name text
banner_image text
date datetime