简体   繁体   中英

How to upload multiple files using codeigniter

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

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/

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);
                        }
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM