2014-09-25 100 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