2016-02-13 81 views
2
public function add_software() 
{ 
    $this->load->library('form_validation'); 

    $this->form_validation->set_error_delimiters('<div class="error-form">', 
'</div>'); 
    $this->form_validation->set_rules('name', 'Name', 
'required|min_length[4]|max_length[25]'); 
    $this->form_validation->set_rules('desc', 'Description', 'required'); 
    $this->form_validation->set_rules('licence', 'Licence.', 'required'); 
    $this->form_validation->set_rules('platform', 'Platform', 'required'); 
    $this->form_validation->set_rules('developers', 'Developer',  
'required'); 
    $this->form_validation->set_rules('link', 'Developer Website', 
'required'); 
    $this->form_validation->set_rules('cat', 'Category', 'required'); 
    $this->form_validation->set_rules('logo', 'Software Logo', 'required'); 

    if ($this->form_validation->run() == FALSE) { 
    $this->load->view('admin/includes/header'); 
    $this->load->view('admin/add_software'); 
    $this->load->view('admin/includes/footer'); 
    } else { 
    //Setting values for tabel columns 
    $data['name']  = $this->input->post('name'); 
    $data['description'] = $this->input->post('desc'); 
    $data['licence']  = $this->input->post('licence'); 
    $data['platform'] = $this->input->post('platform'); 
    $data['developers'] = $this->input->post('developers'); 
    $data['link']  = $this->input->post('link'); 
    $data['category'] = $this->input->post('cat'); 
    $data['logo'] = $this->input->post('logo'); 

    ///////////////////////////////// 
    //set preferences 

    $config = array(
    'upload_path' => '' .base_url(). '/assets/img/logo/', 
    'allowed_types' => "gif|jpg|png|jpeg|pdf", 
    'overwrite' => TRUE, 
    'max_size' => "4096000", 
    'max_height' => "1024", 
    'max_width' => "1024" 
    ); 
      //load upload class library 

    $this->load->library('upload', $config); 
      $this->upload->data(); 
      $this->upload->do_upload(); 
    ///////////////////////////////// 
    //Transfering data to Model 
    $this->insert_model->form_insert($data); 
    $data['message'] = 'Data Inserted Successfully'; 
    //Loading View 
    redirect('admin/view_softwares', 'refresh'); 
    } 
} 

這是我的codeigniter控制器功能。使用此功能,我可以將數據添加到數據庫。但是,我無法上傳圖片。即使upload_files已在php.ini中啓用。我試圖整天解決這個問題,但都是徒勞的。 我沒有在我的HTML形式使用enctype,因爲當我使用enctype,我得到下面的錯誤:使用codeigniter控制器上傳圖像

undefined Index filename 
+0

試 'upload_path'=> './assets/img/logo/', –

+0

你必須使用'ENCTYPE =「多/ form-data「用於上傳文件並使用** $ image_info = $ this-> upload-> do_upload(」name_of_input_type_file「); ** –

回答

1

朋友我西港島線給我已上傳image.This工作肯定做的代碼。 你可以參考this.Any疑問,請諮詢我西港島線幫助您

public function uploadImage() { 
     $this->load->helper(array('form', 'url')); 
     $config['upload_path'] = 'assets/images/b2bcategory'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '1000'; 
     $config['max_width'] = '2024'; 
     $config['max_height'] = '1768'; 
     $config['width'] = 75; 
     $config['height'] = 50; 
     if (isset($_FILES['catimage']['name'])) { 
      $filename = "-" . $_FILES['catimage']['name']; 
      $config['file_name'] = substr(md5(time()), 0, 28) . $filename; 
     } 
     $config['overwrite'] = TRUE; 
     $config['remove_spaces'] = TRUE; 
     $field_name = "catimage"; 
     $this->load->library('upload', $config); 
     if ($this->input->post('selsub')) { 
      if (!$this->upload->do_upload('catimage')) { 
       //no file uploaded or failed upload 
       $error = array('error' => $this->upload->display_errors()); 
      } else { 
       $dat = array('upload_data' => $this->upload->data()); 
       $this->resize($dat['upload_data']['full_path'],   $dat['upload_data']['file_name']); 
      } 
      $ip = $_SERVER['REMOTE_ADDR']; 
      if (empty($dat['upload_data']['file_name'])) { 
       $catimage = ''; 
      } else { 
       $catimage = $dat['upload_data']['file_name']; 
      } 
      $data = array(   
       'ctg_image' => $catimage, 
       'ctg_dated' => time() 
      ); 
      $this->b2bcategory_model->form_insert($data); 

     } 
    } 
+0

謝謝。它的工作 –

+0

歡迎親愛的。總是樂意幫助:) – Angel

0

你的道路是錯誤的。

'upload_path' => '' .base_url(). '/assets/img/logo/', 

Note: Make sure your upload path "assets folder" is in the main directory out side of application folder. You also may need to set folder permissions.

嘗試

'upload_path' => FCPATH . 'assets/img/logo/' 

或只是

'upload_path' => './assets/img/logo/' 

上傳成功後訪問文件信息preferences

$image_info = $this->upload->data(); 

echo $image_info['file_name']; 

或通過型號

$this->insert_model->form_insert($data, $image_info = array());