antroo ghosh antroo ghosh - 11 days ago 6
PHP Question

In code igniter multiple image update

i do multiple image upload in code igniter the same code is used to update the the multiple image but in the update time i change name i don't change images then click ok button the name is update but the images are missing but i change the image the that only update..why?

my controller



function update_products()
{
$id = $this->input->post('id');
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];

$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();

$dataInfo[] = $this->upload->data();

}
$data = array(

'name' => $this->input->post('name'),
'prod_image' => $dataInfo[0]['file_name'],
'prod_image1' => $dataInfo[1]['file_name'],
'prod_image2' => $dataInfo[2]['file_name'],
'upadted_time' => date('Y-m-d H:i:s')
);

$stock = array(
'discount' => $this->input->post('discount'),
);
$this->db->where('prod_id', $id);
$this->db->update('tbl_products', $data);
$this->db->where('prods_id', $id);
$this->db->update('otc_product_discount', $stock);

$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully updated!</div>');
redirect('index.php/admin/display_product_list');

}
}





my view



<label for="type">Product Image</label>
<img src="<?php echo base_url("./resources/images/products/"); if($row->prod_image) echo $row->prod_image; else echo "no-img.jpg"; ?>" alt="" height="50" width="50"/>
<input type="file" id="med_img" class="file_upload" name="userfile[]" value="<?php echo $row->prod_image; ?>" />

<label for="type">Product Image2</label>
<img src="<?php echo base_url("./resources/images/products/"); if($row->prod_image1) echo $row->prod_image1; else echo "no-img.jpg"; ?>" alt="" height="50" width="50"/>
<input type="file" id="med_img" class="file_upload" name="userfile[]" value="<?php echo $row->prod_image1; ?>" />
<label for="type">Product Image3</label>
<img src="<?php echo base_url("./resources/images/products/"); if($row->prod_image2) echo $row->prod_image2; else echo "no-img.jpg"; ?>" alt="" height="50" width="50"/>
<input type="file" id="med_img" class="file_upload" name="userfile[]" value="<?php echo $row->prod_image2; ?>" />





uplaod function is



private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;

return $config;
}




Answer

I thinks need to upload individually like that in your controller.

function update_products() 
{ 
    $id = $this->input->post('id');
    $this->load->library('upload');
    $this->upload->initialize($this->set_upload_options());

    $data = array();

    if($this->upload->do_upload('userfile')) {   
        $dataInfo = $this->upload->data();
        $data['prod_image'] = $dataInfo['file_name'];
    }

    if($this->upload->do_upload('userfile1')) {   
        $dataInfo = $this->upload->data();
        $data['prod_image1'] = $dataInfo['file_name'];
    }

    if($this->upload->do_upload('userfile2')) {   
        $dataInfo = $this->upload->data();
        $data['prod_image2'] = $dataInfo['file_name'];
    }   

    $data['name'] = $this->input->post('name'),
    $data['upadted_time'] = date('Y-m-d H:i:s')  

    $stock = array(
    'discount' => $this->input->post('discount'),
    );

    $this->db->where('prod_id', $id);
    $this->db->update('tbl_products', $data);

    $this->db->where('prods_id', $id);
    $this->db->update('otc_product_discount', $stock);

    $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully updated!</div>');
    redirect('index.php/admin/display_product_list');
} 

Change FIlE input field name userfile userfile1 and userfile2 like bellow :

<input type="file" id="med_img" class="file_upload"  name="userfile"/>
<input type="file" id="med_img" class="file_upload"  name="userfile1"/>
<input type="file" id="med_img" class="file_upload"  name="userfile2"/>