2014-09-25 76 views
0

我有一個表格,如contactemail,contactname,類別,評論截至目前爲止,現在我必須添加圖像和文檔輸入形式,使用形式我已插入數據到數據庫,如何上傳圖像和文檔中的表格codeignatior提交上傳圖像和文檔在表單上提交在php codeignatior

圖像需要圖像文件夾和文件在上傳的文檔文件夾 這裏是我的控制器

public function form() { 

     $this->load->helper('form'); 
     $data = $this->login_model->form(); 
//loading views 
load-view-header 
load view page 
load-view-footer 

} 

這裏我的模型功能

public function form() { 
     $contactemail = $this->input->post('contactemail'); 
     $contactname = $this->input->post('contactname'); 
     $category = $this->input->post('category'); 
     $comments = $this->input->post('comments'); 

     $data = array(
      'contactemail' => $email, 
      'contactname' => $name, 
      'category' => $category, 
      'comments' => $comments 
     ); 
     $this->db->insert('contact', $data); 
     return $data; 
    } 

回答

0

這裏是一個例子。這是迅速整合在一起,完全沒有經過測試...充其量也許有錯誤,最糟糕的是記憶失敗了我,我完全錯過了一些東西,但可能是一個有用的例子。讓我知道它是否有幫助?

查看:

<?php echo $error;?> 

<?php echo form_open_multipart('upload/do_upload');?> 
<!-- your other form fields for email, name, category, comments can go here--> 
<input type="file" name="image" size="20" /> 
<input type="file" name="doc" size="20" /> 
<br /><br /> 

<input type="submit" value="Submit" /> 

</form> 

控制器:

function form() 
    { 
      $this->load->helper(array('form', 'url')); 
      $config['upload_path'] = './images/'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '2048'; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '768'; 

      $this->load->library('upload', $config); 
      $data['error'] = ""; 


      //it would be good to be using the form validation class but here is a quick and dirty check for the submit 

      if (isset($_POST['submit'])) 
      { 

       if (! $this->upload->do_upload('image')) 
       { 
         $data['error'] = $this->upload->display_errors(); 
       } 
       else 
       { 
         $image_upload_data = $this->upload->data(); 
       } 

       unset($config); 
       $config['upload_path'] = './docs/'; 
       $config['allowed_types'] = 'doc|docx|txt'; 
       $config['max_size'] = '2048'; 

       $this->upload->initialize($config); 

       if (! $this->upload->do_upload('doc')) 
       { 
         $data['error'] .= "<br />".$this->upload->display_errors(); 

       } 
       else 
       { 
         $doc_upload_data = $this->upload->data(); 

       } 




       if(!empty($data['error'])) 
       { 
        $this->load->view('form', $data); 
       } 
       else 
       { 
         $contactemail = $this->input->post('contactemail'); 
         $contactname = $this->input->post('contactname'); 
         $category = $this->input->post('category'); 
         $comments = $this->input->post('comments'); 
         $doc_file = $doc_upload_data['full_path']; 
         $image_file = $image_upload_data['full_path']; 

         $form_data = array(
         'contactemail' => $email, 
         'contactname' => $name, 
         'category' => $category, 
         'comments' => $comments, 
         'doc_file' => $doc_file 
       ); 


         $image_data['image_name'] = $image_file; 
         $this->login_model->form($form_data,$image_data); 

         $this->load->view('upload_success', $data); 
       } 

      } 
      else 
      { 
       $this->load->view('form', $data); 
      } 
} 

型號

public function form($form_data,$image_data) { 

     $this->db->insert('contact', $image_data); 

     $image_data['d_fk'] = $this->db->insert_id(); 

     $this->db->insert('images', $image_data); 
    } 
+0

感謝亨利的答覆,因爲我是新來編寫ignatior,需要一個建議,怎麼做,如果該文檔需要插入一個表和圖像表中的圖像,例如:1)圖像表具有id_fk colum和圖像名稱colum和2)表格將具有id和所有f除了基於最後插入的圖像的圖像插入圖像,我需要在圖像表中插入圖像,你能建議嗎? – user4022479 2014-09-26 06:21:06

+0

看到我的編輯,這裏假定你正在插入文件路徑和名稱並將文件存儲在文件系統中 – henry 2014-09-26 07:08:11

+0

非常感謝你亨利 – user4022479 2014-09-26 13:21:26