phpcodeigniteroopcodeigniter-2codeigniter-form-helper

Multiple Images upload in Codeigniter


i ma trying to upload multiple images from HTML FORM but on submit only last image uploaded please any one who can figure out this problem

enter image description here

here is my controller

if($_FILES['image']['name'] != "")
{
    $data['image'] = $this->MUtils->doUpload('image',270,65,false);
}
if($_FILES['adv_image1']['name']!= "")
{
    $data['adv_image1'] = $this->MUtils->doUpload('adv_image1',340,130,false);
}
if($_FILES['adv_image2']['name']!= "")
{
    $data['adv_image2'] = $this->MUtils->doUpload('adv_image2',860,100,false);
}

Model is

    if($data['image']!="" ){
                $arr=array('image' => $data['image']);
            }
            if($data['adv_image1']!=""){
                $arr=array('adv_image1' => $data['adv_image1']);
            }
            if($data['adv_image2']!=""){
                $arr=array('adv_image2' => $data['adv_image2']);
            }

if($data['adv_image3']!=""){
            $arr['adv_image3'] = $data['adv_image3'];
        }

        $this->db->where('id',$data['listid']);
        $this->db->update('list', $arr);

        return 1;

doUpload Functio is here

  //Upload file and return url
    function doUpload($field, $width, $height, $resize=false)
    {
        //Configure upload.
        $this->upload->initialize(array(
            "upload_path"   => "../uploads/",
            "allowed_types" => "gif|jpg|png",
        ));


        //Perform upload.
        if($this->upload->do_upload($field)){

            $fileData = $this->upload->data();

            if ($resize == true)
            {
                $width = $fileData['image_width'];
                $height = $fileData['image_height'];
            }


            $img_cfg_thumb['image_library'] = 'gd2';
            $img_cfg_thumb['source_image'] = "../uploads/" . $fileData['raw_name'] . $fileData['file_ext'];
            $img_cfg_thumb['maintain_ratio'] = FALSE;
            $img_cfg_thumb['new_image'] = "../uploads/" . $fileData['raw_name'] . $fileData['file_ext'];
            $img_cfg_thumb['width'] = $width;
            $img_cfg_thumb['height'] = $height;
            $img_cfg_thumb['quality'] = 90;
            $this->load->library('image_lib');
            $this->image_lib->initialize($img_cfg_thumb);
            $this->image_lib->resize();


            return $fileData['raw_name'] . $fileData['file_ext'];
        }
        else
        {
            return "";
        }
    }

Note this is working, but from three pics just last one is uploaded on submit


Solution

  • In your model change following lines:

    if($data['image']!="" ){
        $arr=array('image' => $data['image']);
    }
    if($data['adv_image1']!=""){
        $arr=array('adv_image1' => $data['adv_image1']);
    }
    if($data['adv_image2']!=""){
        $arr=array('adv_image2' => $data['adv_image2']);
    }
    if($data['adv_image3']!=""){
        $arr=array('adv_image3' => $data['adv_image3']);
    }
    

    To these lines:

    if($data['image']!="" ){
        $arr['image'] = $data['image'];
    }
    if($data['adv_image1']!=""){
        $arr['adv_image1'] = $data['adv_image1'];
    }
    if($data['adv_image2']!=""){
        $arr['adv_image2'] = $data['adv_image2'];
    }
    if($data['adv_image3']!=""){
        $arr['adv_image3'] = $data['adv_image3'];
    }