2014-12-31 93 views
0

我'無法做到上傳圖像與函數add()的代碼行上傳圖像

只是爲我提供了一些解決方案,上傳圖像

公共函數add(){

if ($this->input->server('REQUEST_METHOD') === 'POST') 
    { 


     $this->form_validation->set_rules('image', 'image', 'required'); 
     $this->form_validation->set_rules('text', 'text', 'required'); 
     $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>'); 



     if ($this->form_validation->run()) 
     { 

      **$data_to_insert = array(
       'image' => $this->input->post('image'), 
       'text' => $this->input->post('text'),** 

      ); 

      if($this->home_banner_model->insert_home_banner($data_to_insert)){ 
       $data['flash_message'] = TRUE; 
      }else{ 
       $data['flash_message'] = FALSE; 
      } 

     } 

    } 

    $data['main_content'] = 'admin/home_banner/add'; 
    $this->load->view('includes_admin/template', $data); 
}  
+0

https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html – sinisake

回答

0
$config = array(
     'upload_path' => 'uploadPath', 
     'allowed_types' => 'jpg,jpeg', 
     'max_size'  => 1500 
    ); 

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


     if(isset($_FILES['field_name']) && $_FILES[field_name]['name'] != NULL) 

      if($this->upload->do_upload('field_name')) 
      { 
       $upload_data = $this->upload->data(); 

       $data_to_insert = array(
        'attachment_link' => 'uploadPath/'.$upload_data['file_name'], 
        'attachment_size' => $upload_data['file_size'] 
        // u can add parameters as for ur table to save 
        ); 

       $this->home_banner_model->insert_home_banner($data); 

      } 
      else 
      { 

       echo $this->upload->display_errors(); 

      }