2016-08-17 41 views
1

我想創建一個函數來上傳圖片。我這裏的代碼是在同一時間只有1個文件圖片上傳,我想創建一個數組,可以上傳多張圖片(例臉譜,製作專輯時,你可以上傳多張圖片)如何在文件上傳中創建數組

我控制器

public function addOrganization() 
{ 
    $this->load->view('admin/header'); 
    $this->load->view('admin/sidebar'); 


    $config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $config['max_size'] = '2048000'; 
    $config['overwrite'] = TRUE;   

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

    $this->load->library('form_validation'); 
    $this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>'); 
    $this->form_validation->set_rules('name', 'Name', 'trim|required|is_unique[tblitem.name]'); 
    // $this->form_validation->set_rules('userfile', 'Userfile', 'trim|required|'); 
    $this->form_validation->set_rules('description', 'Description', 'trim|required|min_length[5]'); 


    if ($this->form_validation->run() == FALSE || !$this->upload->do_upload('userfile')) 
    { //if validation is false go to itemList 
     $this->load->view('plmar/admin/addOrganization'); 
    } 
    else 
    { 

     $organization = array(
      'org_name' => $this->input->post('name'), 
      'org_description' => $this->input->post('description'), 
      'org_image' => $this->input->post('userfile'), 
      'upload_data' => $this->upload->data() 
      ); 


     $this->adminModel->addOrganization($organization); //it will add the data of $item in the add function in the model 
     redirect('Administrator/addOrganization','refresh'); 
    } 
    $this->load->view('admin/footer'); 

} 

我的模型

public function addOrganization($organization) 
{ 
    $organization = array(
     'org_name' => $this->input->post('name'), 
     'org_description' => $this->input->post('description'), 

     'org_image' => $this->input->post('userfile'), 
     'org_image' => $this->upload->data('file_name') 
    ); 
    $this->db->insert('tblorganization', $organization); 
} 

我查看

<div class="container-fluid"> 
    <br><br><br><br><br><br><br><br><br> 
    <div class="row"> 
     <div class="col-sm-3"></div> 
     <div class="col-sm-6"> 
      <form method="post" enctype="multipart/form-data" action="<?php echo base_url().'Administrator/addOrganization' ?>"> 

       <div class="form-group"> 
        <label>Name</label> 
        <input type="text" class="form-control" id="name" name="name" placeholder="Organization" value="<?php echo set_value('name'); ?>"> 
        <?php echo form_error('name');?> 
       </div> 

        <div class="form-group"> 
        <label>Image</label> 
        <input type="file" required class="form-control" id="userfile" name="userfile"> 

       </div> 


       <br> 

       <div class="form-group"> 
        <?php echo form_error('userfile');?> 
        <label>Description</label> 
        <textarea class="form-control" rows="3" name="description"><?php echo set_value('description'); ?></textarea> 
        <?php echo form_error('description');?> 
       </div> 


       <button type="submit" class="btn btn-primary">Submit</button> 
      </form> 
     </div> 

    </div> 
+0

你需要創建一個循環,然後重複穿透式它 – SoftwareDev

+0

我編輯我的職務,我把我的模型有 – Holow

回答

0

在您的模型中作爲一個更多參數爲$image以及$organization文件。 $組織插入成功後的 需要last insert id。 再算上第二個參數即$fileCount = count($image["name"]); ,然後for循環

for ($i = 0; $i < $fileCount; $i++){ 
    $imagename = $image["name"][$i]; // will get image name in loop 
    // rest of the move_uploaded_file ,insert into db code goes here 
} 
在模型中
+0

喜歡嗎? $ fileCount = count($ image [「name」]);對於($ i = 0; $ i <$ fileCount; $ i ++){ $ imagename = $ image [「name」] [$ i]; $ organization = array( 'org_name'=> $ this-> input-> post('name'), 'org_description'=> $ this-> input-> post('description'), 'org_image '=> $ this-> input-> post('userfile'), 'org_image'=> $ this-> upload-> data('file_name') ); } – Holow

+0

@我首先在上面的答案中提到你插入其他細節。爲了存儲多個圖像,你需要有單獨的表格。並將圖像名稱與上次插入的組織詳細信息的ID一起存儲。 – SoftwareDev

+0

所以我會有2個表(一個是organization_table,另一個是tblgallery),那麼在我的tblgallery中,這些列是gallery_id和gallery_name? – Holow

相關問題