2

I want to upload multiple files using codeigniter.

I have two types of images beforeimage and afterimage. After uploading each image I make entry in database.

I am creating thumbnail as well. To amke it ease I created a separate function, but its not working. this logic works for single image upload.

Html

controller

foreach($_FILES["beforepicture"]['name'] as $key=>$files){
                        //if(isset($_FILES["beforepicture"]['name']) && $_FILES["beforepicture"]['name'] != ''){
                        $imagestatus = $this->upload_images($files,'beforepicture');
                        if(isset($imagestatus['name'])){
                            $inputdata = array('image' => $imagestatus['name'],
                                'lead_id' =>$inserted_id,
                                'user_id'=>$this->session->userdata['userdata']['userid'],
                                'when'=>'before');
                           $this->common_model->save('lead_images',$inputdata);
                        //}
                        }
                    }

 function upload_images($data = NULL,$inputname=NULL){
        if($data){
            $this->load->library('image_lib');
              //  echo '<pre>';
                  //  print_r($_FILES);
                $new_name = time().$data;
                $config = array(
                'upload_path' => getcwd()."/assets/themes/default/avatars/",
                'allowed_types' => "gif|jpg|png|jpeg",
                'overwrite' => TRUE,
                'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
               // 'max_height' => "768",
                //'max_width' => "1024",
                'file_name' => $new_name
                );
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                //$this->upload->initialize($config);
                if($this->upload->do_upload($inputname))
                {
                 $image_data = $this->upload->data();
                 //thumb1
                 $config = array(
                    'source_image'      => $image_data['full_path'], //path to the uploaded image
                    'new_image'         => getcwd()."/assets/themes/default/avatars/thumb", //path to
                    'maintain_ratio'    => true,
                    'width'             => 180,
                    'height'            => 200
                    );
                 $this->image_lib->initialize($config);
                 $this->image_lib->resize();  
                 $message = array('name' => $image_data['file_name']);
                }else
                {
                    //echo $this->upload->display_errors(); die;
                    $message = array('failed' => $this->upload->display_errors());
                }

                return $message;
        }
    }

I am trying but failed

1
  • please add form code as well Commented Oct 9, 2015 at 11:11

2 Answers 2

1

Try this

 <?php
  class My_Controller extends CI_Controller {
  public function __construct() {
  parent::__construct();
$this->load->library('upload');
}
              $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();
                $fileName = $_FILES['userfile']['name'];
                 $images[] = $fileName;
}
  $fileName = implode(',',$images);
  $this->my_model->upload_image($fileName);
}
private function set_upload_options()
  { 
  // upload an image options
         $config = array();
         $config['upload_path'] = './upload/'; //give the path to upload the image in folder
         $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '0';
         $config['overwrite'] = FALSE;
  return $config;
  }
}

For more how to upload multiple file in codeigniter try this tutorial:http://w3code.in/2015/09/upload-file-using-codeigniter/

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

$files = $_FILES;
                       $cpt = count($_FILES['beforepicture']['name']);
                       for($i=0; $i<$cpt; $i++){
                           $_FILES['beforepicture']['name']= $files['beforepicture']['name'][$i];
                            $_FILES['beforepicture']['type']= $files['beforepicture']['type'][$i];
                            $_FILES['beforepicture']['tmp_name']= $files['beforepicture']['tmp_name'][$i];
                            $_FILES['beforepicture']['error']= $files['beforepicture']['error'][$i];
                            $_FILES['beforepicture']['size']= $files['beforepicture']['size'][$i];
                        $imagestatus = $this->upload_images($_FILES["beforepicture"]['name'],'beforepicture');
                        if(isset($imagestatus['name'])){
                            $inputdata = array('image' => $imagestatus['name'],
                                'lead_id' =>$inserted_id,
                                'user_id'=>$this->session->userdata['userdata']['userid'],
                                'when'=>'before');
                           $this->common_model->save('lead_images',$inputdata);
                        }
                    }

1 Comment

above both are the solution, but this has modified as per requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.