2015-05-02 23 views
0

我想直到今天我用這個代碼上傳單個圖像如何從單一的輸入笨上傳多張圖片的Fileds

對我的畫廊 上傳多張圖片

我控制器

public function addPhoto($album_id) 
     { 
      if ($this->input->post('submit')) { 
       $this->form_validation->set_rules('caption', 'Caption', 'required'); 
       $this->form_validation->set_rules('userfile', 'Image', 'callback__checkImage'); 

       if ($this->form_validation->run()) { 
        $config['upload_path'] = $this->config->item('upload_path'); 
        $config['allowed_types'] = $this->config->item('allowed_types'); 
        $config['max_size'] = $this->config->item('max_size'); 
        $config['max_width'] = $this->config->item('max_width'); 
        $config['max_height'] = $this->config->item('max_height'); 
        $config['encrypt_name'] = $this->config->item('encrypt_name'); 

        $this->load->library('upload', $config); 

        if ($this->upload->do_upload()) { 
         $image = $this->upload->data(); 

         $this->album_model->addPhoto($image['file_name'], $album_id); 
         $this->session->set_flashdata('msg', 'Photo added successfully.'); 
         redirect('album/photos/'.$album_id); 
        } else { 
         $this->session->set_flashdata('msg', $this->upload->display_errors()); 
         redirect('album/addPhoto/'.$album_id); 
        } 
       } 
      } 

      $data['title'] = 'Add Photo'; 
      $data['content'] = $this->load->view('album/add_photo', '', true); 
      $this->load->view('template', $data); 
     } 

我車型

function addPhoto($image, $album_id) 
    { 
     $this->db->set('name', $image); 
     $this->db->set('caption', $this->input->post('caption')); 
     $this->db->set('album_id', $album_id); 
     $this->db->insert('photos'); 
    } 

和我的看法是

<form action="" method="post" enctype="multipart/form-data"> 
    <h1>Add Photo</h1> 
    <?php echo $this->session->flashdata('msg'); ?> 

    <div> 
     <label>Image</label> 
     <input type="file" name="userfile"> 
     <?php echo form_error('userfile'); ?> 
    </div> 
    <br> 

    <div> 
     <label>Caption</label> 
     <input type="text" name="caption" value="<?php echo set_value('caption'); ?>" placeholder="Caption"> 
     <?php echo form_error('caption'); ?> 
    </div> 
    <br> 

    <div> 
     <label></label> 
     <button type="submit" name="submit" value="submit">Upload</button> 
    </div> 
</form> 

這就是我用來上傳單張圖片現在我想上傳多個,我只有一個圖片字段上的數據庫,我需要做更多的圖片字段上傳多張圖片?

回答

0

你得送<input type="file" name="userfile"> 作爲一個數組,像這樣:

<input type="file" multiple name="userfile[]"> 

然後對每個投稿的圖像做一個上傳。

BTW本網站上快速搜索給了我這個職位,有其他所有信息,你需要的是: Multiple files upload (Array) with CodeIgniter 2.0

好運

0

如果您要上傳多張圖片有針對

Blueimp

這是一個很簡單,很偉大的插件我還用它在5月的項目這麼多的插件。

+0

它的jQuery插件它多張圖片發到我databsae ???? –

+0

是的,但你必須編碼,它會發送所有上傳的文件的詳細信息到你的行動。 –