如何使用codeigniter上传多个文件

时间:2015-10-09 11:07:13

标签: php codeigniter codeigniter-2

我想使用codeigniter上传多个文件。

我有两种类型的图像在图像和残像之前。上传每张图片后,我在数据库中输入。

我也在制作缩略图。为了方便起见,我创建了一个单独的函数,但它不起作用。此逻辑适用于单个图像上载。

HTML

                                                                                                                                                                                                                                                                                                                                                                                      

控制器

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

我正在尝试但失败了

2 个答案:

答案 0 :(得分:1)

试试这个

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

有关如何在codeigniter中上传多个文件的更多信息,请参阅本教程:http://w3code.in/2015/09/upload-file-using-codeigniter/

答案 1 :(得分:0)

试试这个

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