How to upload 2 separate images in codeigniter
Asked Answered
L

4

6

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);
    }
}
Louvenialouver answered 7/4, 2016 at 6:48 Comment(3)
You can just use one file upload button, and add multiple="multiple" attribute in the button and differentiate between the file type on server side.Elflock
no i need two buttons not one that is the requirement of our clientLouvenialouver
You can take a look at this answer and upload based on the file type.Elflock
S
1

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');
    }   
}
Seagoing answered 7/4, 2016 at 10:18 Comment(0)
I
1

This is the controller code i Have applied for uploading two image in codeigniter

public function index() {

if($this->input->post('Submit')){

//-----------Image File Section Start Here -----------//

 $config['upload_path'] = './uploads/';   // Directory 
$config['allowed_types'] = 'jpg|jpeg|bmp|png';  //type of images allowed
$config['max_size'] = '30720';   //Max Size
$config['encrypt_name'] = TRUE;   // For unique image name at a time

$this->load->library('upload', $config);  //File Uploading library
$this->upload->do_upload('userfile');  // input name which have to upload 
$video_upload=$this->upload->data();   //variable which store the path

//--------------End of Image File  Section------------------------//



//---------Thumbnail Image Upload Section Start Here -----------//

$config2['upload_path'] = './thumb/';   // Directory 
$config2['allowed_types'] = 'jpg|jpeg|bmp|png'; //type of images allowed
$config2['max_size'] = '30720';   //Max Size
$config2['encrypt_name'] = TRUE;   // For unique image name at a time


$this->upload->initialize($config2); //we can not use upload library again and again it will not initialize again and again so thats why i have used initialize 
$this->upload->do_upload('txt_thumb');  // File Name
$thumbnail_upload=$this->upload->data(); // store the name of the file 

//--------End of Thumbnail Upload Section-----------//



    $date=date("d-m-Y");  // Store current date in variable 

   // Here the database query to insert

    $data = array(
    'parent_id'=> $this->input->post('txt_parent'),
    'cat_id' => $this->input->post('txt_category'),
    'title'=> $this->input->post('txt_title'),
    'status' => $this->input->post('txt_status'),
    'featured' => $thumbnail_upload['file_name'],
    'image' => $video_upload['file_name'],
    'time'=>$date
    );

       $sql_ins= $this->Insimage->insertimage($data);
       if($sql_ins)
       {
           $data['Success'] = "Image has been succesfully inserted!!";
       }

}

This code will surely work to upload 2 images Enjoy!!!! :-)

Irruptive answered 8/9, 2017 at 14:35 Comment(0)
C
0
<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" multiple="">
<input type="submit" name="submit" value="upload">
</form>
Craftsman answered 7/4, 2016 at 7:0 Comment(3)
sir i need two buttons not oneLouvenialouver
and need to separately upload the imagesLouvenialouver
in one button you need to upload single image and in other multiple image..right?Craftsman
S
0

1- create image_uploader in your controller

function image_uploader($filename){
  $config['upload_path'] = './assets/uploads/setting/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '2000';
  $config['max_height']  = '2000';
  $this->load->library('upload', $config);
  if ( ! $this->upload->do_upload($filename)) {
  $error = array('error' => $this->upload->display_errors());
  } else {
  $data_foto = $this->upload->data();
  return $data_foto['file_name'];
}

this function return name of image file , if upload was successfully

2- call image_uploader in other function (form action in view )

public function slideroneupload() {
  $this->site_security->_make_sure_is_admin();
  $data = $this->input->post();
  $sliderimage = $this->image_uploader('image');
  $thumbimage = $this->image_uploader('thumb');

  // save all data to database 
  .
  .
  .

}
Spreader answered 3/11, 2019 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.