我是codeIgniter的新手,我正在尝试进行注册表单,并上传个人资料图片并将图片名称存储在数据库中。
所以当我使用form_open
时,它存储在数据库上就好了,但文件没有进入我的目录,当我使用form_open_multipart
图像上传但查询设置为null并且所有操作都没有。
<?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 :(得分:0)
我的错误是,$this->input->post('profile_pic')
并且文件数据没有存储在$ _Post它在$ _File上
所以解决方案是
$data_upload_files = $this->upload->data();
$image = $data_upload_files['full_path'];
$image
转到数据库