在codeigniter中上传多个图片

时间:2017-10-03 07:50:49

标签: php codeigniter

我想在一个提交中上传多个图像,这里我想将图像名称保存在数据库中的两个单独的字段中。下面给出的代码是我用于插入单个图像。我尝试通过更改名称重复图像上传代码两次来解决这个问题,但是我知道有没有正确的解决方法。

<input type="file" class="upload" name="picture" />

public function image_upload()
{
    if($this->input->post('submit')=="save")
    {
        //Check whether user upload picture
        if(!empty($_FILES['picture']['name']))
        {
            $config['upload_path'] = 'uploads/images/banner/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['file_name'] = $_FILES['picture']['name'];

            //Load upload library and initialize configuration
            $this->load->library('upload',$config);
            $this->upload->initialize($config);

            if($this->upload->do_upload('picture'))
            {
                $uploadData = $this->upload->data();
                $picture = $uploadData['file_name'];
            }

             //Prepare array of user data
            $userData = array('package_name' => $this->input->post('package_name'),'picture' => $picture); 

            //Pass user data to model
            $insertUserData = $this->gt_banner_model->insert_image($userData);
        }

        //Storing insertion status message.
       if(isset($insertUserData))
             {
                $data['message']="* Image inserted successfully.";

             }
             else
             {
                $data['message']="* Some problems occured, please try again.";
             }
    }
    //Form for adding user data
    $query = $this->db->get("gt_banner");
    $data['records'] = $query->result();
    $this->load->view('admin/banner/banner_insert',$data);
}

2 个答案:

答案 0 :(得分:0)

在html中

<input type="file" class="upload" multiple="" name="images[]"> 

并确保您的表单中包含属性multipart:

<form enctype="multipart/form-data" method="post" action="your_controller_path/upload_files">

在控制器中:

public function upload_files(){
        if($this->input->post('submit')){
            //print_r($_POST);
            if(($_FILES['images'])){
                  $errors= array();
                  $file_name = $_FILES['images']['name'];
                  $file_size = $_FILES['images']['size'];
                  $file_tmp = $_FILES['images']['tmp_name'];
                  $file_type = $_FILES['images']['type'];
                  $ext = pathinfo($file_name, PATHINFO_EXTENSION);
                  $expensions= array("jpeg","jpg","png");
                  $mtime = uniqid(microtime());
                     $uniqueid =  substr($mtime,2,8);
                     $filename = $uniqueid.'.'.$ext;
                    if(in_array($ext,$expensions)=== false){
                       $errors[]="extension not allowed, please choose a JPEG or PNG file.";
                    }
                    if($file_size > 2097152) {
                       $errors[]='File size must be excately 2 MB';
                    }
                    if(empty($errors)==true) {
                        move_uploaded_file($file_tmp,"uploads/".$filename);
                    }
            }


            $data = array(
                    'imagename'=> $filename,
                    'created'=> date('Y-m-d h:i:s')
                );
            $this->db->insert('user_detail', $data);
            $insert_id = $this->db->insert_id();
                if($insert_id){
                    $this->session->set_flashdata('success', 'Image uploaded successfully');
                    redirect('Current_controller_index');
                }else{
                    $this->session->set_flashdata('error', 'Some error occur');
                    redirect('Current_controller_index');
                }
        }
    }

答案 1 :(得分:0)

我在Uploading multiple images in CodeIgniter

解决了同样的问题

按照这一点,它可以帮助你。