0

I want to upload 3 images and create thumb of them and want to store their name in databse . Any idea what to do.NOte I have read many forum and questions but none has helped me in doing both upload and store in database.Here is what i have done for 1 image,it would help me if i can do it same way for multiple images with little modification

View

<p>
    <label>Image 1</label>
    <input type="file" name="userfile"  value=""/>

// I want same kind of anothe two uploads say image2,image3
</p>

Controller

function insert()
{
$data = array('title'=>'Add New Image',
'link_add'=>site_url('manage/img_gallaryslider/add'),
'edit_link'=>site_url('manage/img_gallaryslider/edit'), 'action'=>site_url('manage/img_gallaryslider/insert'),
'link_back'=>site_url('manage/img_gallaryslider'),
'tbl'=>'imgslider'); 
$this->_set_fields();
$this->_set_rules();
if ($this->form_validation->run() == TRUE)
    {
       $config['upload_path'] = './images/imagegallaryslider';
       $config['allowed_types'] = 'gif|jpg|png';
       $config['max_size']  = '1000';
       $config['max_width']  = '1024';
       $config['max_height']  = '768';
       $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            if($_FILES['userfile']['name']=='')
        {
         $data['upload_error']='<strong>Error: Please, select image to upload</strong>';
        }
       else
        {
            $data['upload_error']='<strong>Error:Invalid file format or size</strong>';
        }
        $this->load->view('manage/includes/header', $data);
        $this->load->view('manage/img_gallaryslideredit', $data);
        $this->load->view('manage/includes/footer');
        }
        else
        {
          $data = array('upload_data' => $this->upload->data());
          $filename= $this->upload->data();
          $file_name=$this->upload->file_name;
              $this->create_thumb($file_name);
         // save data

        $this->ip_date = $this->admin_model->get_date_ip();
        $value_array = array('Name' => $this->input->post('name'),
                             'Image'=>$this->upload->file_name,
                         'CreatedBy' => $this->session->userdata('adminid'),
                         'CreatedDate'=>$this->ip_date->cur_date,
                         'CreatedIp'=>$this->ip_date->ip);
    $id = $this->admin_model->save('imggallaryslider',$value_array);
    $this->session->set_flashdata('notification',$this->lang->line('gen_succ_added'));
    redirect(site_url('manage/img_gallaryslider/index'));
    die();
    }
    }
     else
        {
        $this->load->view('manage/includes/header', $data);
        $this->load->view('manage/img_gallaryslideredit', $data);
        $this->load->view('manage/includes/footer');
    }

}
function create_thumb($file_name)
     {
        $config['image_library'] = 'gd2';
        $config['source_image'] = './images/imagegallaryslider/'.$file_name;    
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 70;
        $config['height'] = 50;
        $config['new_image'] = './images/imagegallaryslider/thumb/'.$file_name;
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
     }

model

function save($table,$value)
    {
    $this->db->insert($table, $value);
    return $this->db->insert_id();
    }    
</code>
Nishant Lad
  • 616
  • 2
  • 7
  • 19
  • Check [here](http://www.web-and-development.com/image-crud-an-automatic-multiple-image-uploader-for-codeigniter/) – mamdouh alramadan Jan 19 '13 at 05:15
  • Did you got any error ? May be it will help you to split task to 3 different task – zb' Jan 19 '13 at 05:16
  • i think `create_thumb()` and bigest part of `insert()` should be in model – zb' Jan 19 '13 at 05:17
  • @eicto the code i have posted for single file,its working good no error,but now i want it for three files – Nishant Lad Jan 19 '13 at 05:18
  • @eicto this is how we write in codeigniter all function call in controller and sql methods in model – Nishant Lad Jan 19 '13 at 05:20
  • @NishantLad better to use libraries or models when you process data, database is just a sample of data. so controllers - public methods, models - data processing, views - display templates, libraries - utilites – zb' Jan 19 '13 at 05:59

1 Answers1

1

The method do_upload() declared as public function do_upload($field = 'userfile'), so you can use it in your code like:

private function uploads() {
   $config['upload_path'] = './images/imagegallaryslider';
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size']  = '1000';
   $config['max_width']  = '1024';
   $config['max_height']  = '768';
   $this->load->library('upload', $config);
   //<--HERE the several images handler
   foreach (array('userfile','img1','img2' as $field) { 
    if ( ! $this->upload->do_upload($field))
    {
        $error = array('error' => $this->upload->display_errors());
        if($_FILES[$field]['name']=='')
    {
     $data['upload_error']='<strong>Error: Please, select image to upload</strong>';
    }
   else
    {
        $data['upload_error']='<strong>Error:Invalid file format or size</strong>';
    }
    return $data;
   }
   $data = array('upload_data' => $this->upload->data());
   $filename= $this->upload->data();
   $file_name=$this->upload->file_name;
   $this->create_thumb($file_name);
     // save data
   $this->ip_date = $this->admin_model->get_date_ip();
   $value_array = array('Name' => $this->input->post('name'),
                        'Image'=>$this->upload->file_name,
                     'CreatedBy' => $this->session->userdata('adminid'),
                     'CreatedDate'=>$this->ip_date->cur_date,
                     'CreatedIp'=>$this->ip_date->ip);
   $id = $this->admin_model->save('imggallaryslider',$value_array);
 }
 $this->session->set_flashdata('notification',$this->lang->line('gen_succ_added'));
 redirect(site_url('manage/img_gallaryslider/index'));
 die();
}

in view it should look like:

<p>
  <label>Image 1</label>
  <input type="file" name="userfile"  value=""/>
  <input type="file" name="img1"  value=""/>
  <input type="file" name="img2"  value=""/> 
</p>
zb'
  • 8,071
  • 4
  • 41
  • 68
  • that means i have to edit above code in my upolad library????.and what abt the insert in database – Nishant Lad Jan 19 '13 at 05:57
  • hm i made `uploads` just a method, to be able to return after first error. – zb' Jan 19 '13 at 06:02
  • about the insert to database, `$this->myimagemodel->insert($filename)` is not enough ? You asked 3 questions in one, it is not possible to make answer on all of them at once, try some, ask new questions, not ask me code for you – zb' Jan 19 '13 at 06:04
  • i didnt ask you to code bdw...i have already posted a code..i just want modification in there as i have mentioned – Nishant Lad Jan 19 '13 at 06:06
  • i did modifications, exept database, as you not pasted your model methods you can break loop btw, without new method in controller/model, i just give you a sample – zb' Jan 19 '13 at 06:07
  • here i have posted model code..you can see i have edited question – Nishant Lad Jan 19 '13 at 06:10
  • yeah...i want this now...as i have inserted 'image'->$this->upload->file_name, how can i insert other two 'image1',and 'image2' ..these are my database column name – Nishant Lad Jan 19 '13 at 06:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23006/discussion-between-eicto-and-nishant-lad) – zb' Jan 19 '13 at 06:28