2017-07-29 73 views
0

我是codeIgniter中的新手,我正在嘗試註冊表單,並上傳配置文件圖片並將圖像名稱存儲在數據庫中。 所以當我使用form_open它存儲在數據庫上就好了,但文件不會去我的目錄,當我使用form_open_multipart圖像上傳但查詢設置爲空,並且所有操作下降。codeIgniter上傳圖片故障碼

<?php 
class User extends CI_Controller{ 

    public function __construct() 
    { 
    parent::__construct(); 
     $this->load->helper('url_helper'); 
     $this->load->model('user_model'); 
     $this->load->helper('form'); 
     $this->load->helper('date'); 
     $this->load->library('form_validation');   
    } 

    public function sign_up(){ 
     $this->form_validation->set_rules('name', 'Username', 'trim|required|min_length[3]|max_length[20]'); 
     $data['title']='Sign Up Page'; 
     $config = array(

      array(
      'field'=>'email', 
      'label'=>'Email', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'mobile', 
      'label'=>'Mobile', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'flat_number', 
      'label'=>'flat number', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'street_number', 
      'label'=>'street number', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'street', 
      'label'=>'street', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'area', 
      'label'=>'area', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'governor', 
      'label'=>'governor', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'name', 
      'label'=>'Name', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'birth_day', 
      'label'=>'birth day', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'birth_month', 
      'label'=>'birth month', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'birth_year', 
      'label'=>'birth year', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'gender', 
      'label'=>'gender', 
      'rules'=>'required' 
        ), 
      array(
      'field'=>'user_type', 
      'label'=>'user type', 
      'rules'=>'required' 
        ), 
      ); 
     $this->form_validation->set_rules($config); 
     $this->form_validation->set_rules('password', 'Password', 'required'); 
     $this->form_validation->set_rules('password_comfirm', 'Password Confirmation', 'required|matches[password]'); 
     $config['upload_path']   = './uploads/'; 
     $config['allowed_types']  = 'gif|jpg|png'; 
     $config['max_size']    = 1000; 
     $config['max_width']   = 1024; 
     $config['max_height']   = 1024; 
     $this->load->library('upload', $config); 
     $this->upload->do_upload('profile_pic'); 
     if($this->form_validation->run() === false){ 

      $this->load->view('user/sign_up',$data); 
     } 
     else{ 
      $data = array('upload_data' => $this->upload->data()); 

      $this->user_model->add_user(); 
      $this->load->view('main_pages/succes'); 
     } 

    } 

} 

<?php 

類User_model擴展CI_Model {

public function __construct(){ 
    $this->load->database(); 
} 

public function add_user(){ 
    $datestring = '%d-%m-%Y'; 
    $time = time(); 
    $sign_up_date = mdate($datestring, $time); 
    $address = $this->input->post('flat_number').'-'.$this->input->post('street_number').'-'.$this->input->post('street').'-'.$this->input->post('area'); 
    $birthdate = $this->input->post('birth_day').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_year'); 
     $data = array(
      'name'   => $this->input->post('name'), 
      'sign_up_date' => $sign_up_date, 
      'email'  => $this->input->post('email'), 
      'mobile'  => $this->input->post('mobile'), 
      'address'  => $address, 
      'password'  => $this->input->post('password'), 
      'birthdate' => $birthdate, 
      'gender'  => $this->input->post('gender'), 
      'user_type' => $this->input->post('user_type'), 
      'pic_name'  => $this->input->post('profile_pic'), 
      'governor'  => $this->input->post('governor') 
     ); 
    return $this->db->insert('user',$data); 
} 

}

回答

0

我的錯誤是,$this->input->post('profile_pic') 和文件數據不存儲在$ _ POST它在$ _File 所以解決的辦法是

$data_upload_files = $this->upload->data(); 
$image = $data_upload_files['full_path']; 

$image去數據庫