Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload 2 separate images in codeigniter

i want to upload 2 image separately. i have a condition in which i need to upload file and some time bohth and some time only image i need to use separate button how to differentiate both image and file.

my codes are

<form action="http://localhost/cod_login/club/test2" enctype="multipart/form-data" method="post" accept-charset="utf-8">
  <input type="file" name="userfile" size="20">
  <input type="file" name="userfile" size="20">
  <input type="submit" name="submit" value="upload">
</form>

this is controller

function ddoo_upload(){
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $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());
        $this->load->view('upload_form', $error);
    } else {
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('upload_success', $data);
    }
}
like image 386
sheraz iqbal Avatar asked Apr 07 '16 06:04

sheraz iqbal


1 Answers

If you want 2 different file button, you need to give them different names.

<form action="" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="file" name="userfile1" size="20">
<input type="file" name="userfile2" size="20">
<input type="submit" name="submit" value="upload">

Than you have to modify your function ddoo_upload() like below :-

  function ddoo_upload($filename){
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload($filename)) {
        $error = array('error' => $this->upload->display_errors());
    return false;
    // $this->load->view('upload_form', $error);
    } else {
    $data = array('upload_data' => $this->upload->data());
    return true;
    //$this->load->view('upload_success', $data);
    }
}

NOTE:- We are passing $filename as variable and than using it to upload different files.

Now in controller where the form action is redirecting, you need to write below code.

  if ($this->input->post('submit')){
    if (isset($_FILES['userfile1']) && $_FILES['userfile1']['name'] != ''){
        $file1 = $this->ddoo_upload('userfile1');
    }
        
    if (isset($_FILES['userfile2']) && $_FILES['userfile2']['name'] != ''){
        $file2 = $this->ddoo_upload('userfile2');
    }   
}
like image 149
Rima Avatar answered Oct 02 '22 09:10

Rima