簡體   English   中英

在Codeigniter中上傳多張圖片

[英]Multiple image upload in 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);
}

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

我已經在這里解決了相同的問題, 在CodeIgniter中上傳多個圖像

遵循此步驟,可能會對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM