Skip to Content
PHPCODE
CodeIgniter Image File Upload
codeigniter code / August 8, 2018

Image File Upload in Codeignter

alter TABLE articles add COLUMN image_path varchar(255)

step 1 : add_articl.php in view add this code

<?php echo form_open_multipart(‘admin/store_article’);?>
<div class=”row”>
<div class=”col-lg-12″>
<?php echo form_label(‘Select Image’);?>
</div>
</div>
<div class=”row”>
<div class=”col-lg-6″>
<div class=”form-group”>
<?php
echo form_upload  ([‘class’=>’form-control’,’name’=>’userfile’]);
?>
</div>
</div>
<div class=”col-lg-6″>
<?php  if(isset($upload_error)) echo $upload_error?>
</div>
</div>

default this name file userfile

step 2 :- controller in function add code

public function store_article()
{
$config=[
‘upload_path’ => ‘./uploads’,
‘allowed_types’ => ‘jpg|gif|png|jpeg’,

];
$this->load->library(‘upload’,$config);
$this->load->library(‘form_validation’);

if($this->form_validation->run(‘add_article_rules’) && $this->upload->do_upload())
{
$post=$this->input->post();
$data=$this->upload->data();
echo “<pre>”;
//print_r($data);exit;
$image_path=base_url(“uploads/”.$data[‘raw_name’].$data[‘file_ext’]);
//echo ($image_path);die();
$post[‘image_path’]=$image_path;
return $this->_falshAndRedirect(
$this->articles->add_article($post),
“Article Added successfull.”,
“Article failed To Add, Please Try Again.”
);
}
else
{
$upload_error=$this->upload->display_errors();
$this->load->view(‘admin/add_article’,compact(‘upload_error’));
}
}

or
//$this->load->library(‘upload’,$config);
$this->load->library(‘upload’);
$this->upload->initialize($config)

PHPCODE © 2024