I am trying to upload two images in database at once submit attempt from single form that have two different file input fields. I tried but its not working when i try to show result using print_r it gives a single file name for both input fields. How can i do it perfectly in codeigniter. please help for your reference i am uploading my code. you if any error please let me know.
HTML Form Code
<form id="form_edit" method="post" action="<?php echo base_url(" admin/aboutus/update/".$aboutus->id); ?>" enctype="multipart/form-data" accept-charset="utf-8">
<div class="form-group row">
<div class="col-md-4 col-xs-4">
<input type="text" name="imgtitle" title="About Us Image Title" class="form-control input-sm" value="<?php echo set_value('imgtitle', $aboutus->imgtitle); ?>" placeholder="Image Title" required="required">
<?php echo form_textarea(['rows'=>'15', 'name'=>'imgdetail','title'=>'About Us Image Description','class'=>'form-control mptop input-sm','required'=>'required','value'=> set_value('detail', $aboutus->imgdetail)]); ?>
<input type="file" name="img" title="About Us Image" class="form-control input-sm">
</div>
<div class="col-md-4 col-xs-4">
<input type="text" title="About Us Title" class="form-control input-sm" name="title" value="<?php echo set_value('title', $aboutus->title); ?>" placeholder="Type left side title of about us of maximum 15 characters" required="required">
<?php echo form_textarea(['rows'=>'15', 'name'=>'detail','title'=>"About Us Detail",'class'=>'form-control mptop input-sm mptop','required'=>'required','value'=> set_value('detail', $aboutus->detail)]); ?>
</div>
<div class="col-md-4 col-xs-4">
<input type="text" name="img_2title" title="About Us Image Title" class="form-control input-sm" value="<?php echo set_value('imgtitle', $aboutus->img_2title); ?>" placeholder="Image Title" required="required">
<?php echo form_textarea(['rows'=>'15', 'name'=>'img_2detail','title'=>'About Us Image Description','class'=>'form-control mptop input-sm','required'=>'required','value'=> set_value('detail', $aboutus->img_2detail)]); ?>
<input type="file" name="img_2" title="About Us Image" class="form-control input-sm">
<!-- id="detail" -->
</div>
</div>
<div class="form-group row">
<div class="col-md-12 col-xs-12 mptop rmzero rpZero">
<div class="btn-group pull-right text-right">
<a href="<?php echo base_url('admin/dashboard');?>" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back</a>
<!-- <a href="<?= base_url('fassets/images/aboutus/'.$aboutus->img); ?>" class="btn btn-info btn-sm" name="preview" id="save"><i class="fa fa-search"></i> Preview</a> -->
<button type="submit" class="btn btn-success btn-sm" name="submit" id="save"><i class="fa fa-save"></i> Save</button>
</div>
</div>
</div>
</form>
My Codeigniter Control Code
public function update($id)
{
$res = array();
//form field validation rules
$this->form_validation->set_rules('title', 'Title', 'required|max_length[15]');
if (!$this->form_validation->run()) {
echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
exit;
}
$this->form_validation->set_rules('detail', 'Detail', 'required|max_length[2000]');
if (!$this->form_validation->run()) {
echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
exit;
}
$this->form_validation->set_rules('imgtitle', 'Image Title', 'required|max_length[15]');
if (!$this->form_validation->run()) {
echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
exit;
}
$this->form_validation->set_rules('imgdetail', 'Image Description', 'required');
if (!$this->form_validation->run()) {
echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
exit;
}
$this->form_validation->set_rules('img_2title', 'Second Image Title', 'required|max_length[15]');
if (!$this->form_validation->run()) {
echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
exit;
}
$this->form_validation->set_rules('img_2detail', 'Second Image Description', 'required');
if (!$this->form_validation->run()) {
echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
exit;
}
$config['upload_path'] = 'fassets/images/aboutus';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
//$config['file_name'] = $_FILES['img']['name'];
$config['overwrite'] = TRUE;
//Load upload library and initialize configuration
$this->load->library('upload', $config);
// echo $image_path; exit;
if((!$this->upload->do_upload('img')) && (!$this->upload->do_upload('img_2')))
{
$userData = array(
'title' => $this->input->post('title'),
'detail' => $this->input->post('detail'),
'imgtitle' => $this->input->post('imgtitle'),
'imgdetail' => $this->input->post('imgdetail'),
'img_2title' => $this->input->post('img_2title'),
'img_2detail' => $this->input->post('img_2detail')
);
//Pass user data to model
$insertUserData = $this->AboutusModel->update($userData, $id);
}
else
{
$userData = array(
'title' => $this->input->post('title'),
'detail' => $this->input->post('detail'),
'imgtitle' => $this->input->post('imgtitle'),
'imgdetail' => $this->input->post('imgdetail'),
'img_2title' => $this->input->post('img_2title'),
'img_2detail' => $this->input->post('img_2detail'),
'img' => $this->upload->data('file_name'),
'img_2' => $this->upload->data('file_name')
);
echo "<pre>";
print_r($userData);
exit;
//Pass user data to model
$insertUserData = $this->AboutusModel->update($userData, $id);
}
//Storing insertion status message.
if($insertUserData){
$res = array(
'mes' => 'text-success',
'msg' => "Record has been saved successfully.",
);
echo json_encode($res);
} else {
$res = array(
'mes' => 'text-danger',
'msg' => "Oops! Something went wrong.",
);
echo json_encode($res);
}
}