使用CodeIgniter添加上传图像字段以注册表单 [英] Add upload image field to register form using CodeIgniter

查看:113
本文介绍了使用CodeIgniter添加上传图像字段以注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 CodeIgniter 中添加上传图片字段以注册表单。 / p>

我在控制器中的注册码:

  function add_new_ticket 

if($ this-> input-> post('fullname',TRUE)
&& $ this-> input-& TRUE)
&& $ this-> input-> post('age',TRUE)
&& $ this-> input-& TRUE)
//&& $ this-> input-> post('isFileID',TRUE)
//&& $ this-> input-> post 'FileID',TRUE)
&& $ this-> input-> post('phone',TRUE)
&& $ this-> input-> post 'type',TRUE)
&& $ this-> input-> post('opsType',TRUE)
&& $ this-> input-> post 'clienc',TRUE)
&& $ this-> input-> post('dr',TRUE)
&& strlen($ this-> input-> post('phone',TRUE))> = 9
&&& strlen($ this-> input-> post('nID',TRUE))> = 9
&& (int)$ this-> input-> post('age',TRUE)< = 99
&& count(explode(' - ',$ this-> input-> post('default-date',TRUE))> 1)
){
/ pre>

还有我的模型文件中的代码:

  ;?php 
class user extends CI_Model {

public function add(){
$ data = array(
'name'=> $ this-> input('password'),
'add_date'=> time(),可以使用下面的命令:input-> post('name'),
'password'=> $ this-> input-> ,
'email'=> $ this-> input-> post('email'),
'birth_date'=> $ this-> input-> post '),
'phone'=> $ this-> input-> post('phone'),
'mobile'=> $ this-> input-> post 'mobile'),
'sex'=> $ this-> input-> post('type'),
'city'=> $ this-> input-> post('city'),
'first_name'=> $ this-> input-> post('first_name'),
'last_name'=& $ this-> input-> post('last_name'),
'main_street'=> $ this-> input-> post('main_street'),
'sub_street'=> $ this-> input-> post('sub_street'),
'type'=> $ this-> input-> post('member_type'),
'delegate_name'=> $ this-> input-> post('delegate_name'),
'delegate_email'=> $ this-> input-> post('delegate_email'),
'delegate_pass'=> md5($ this-> input-> post('delegate_pass')),
'location'=> serialize(array($ this-> input-> post('lat'),$ this-> input-> post('lng')))
);
$ this-> db-> insert('admins',$ data);
}

public function edit($ id = FALSE){

$ this-> db-> set('name',$ this-> ; input-> post('name'));

$ this-> db-> set('email',$ this-> input-> post('email'))
$ this-> db-> set('phone',$ this-> input-> post('phone'));
$ this-> db-> set('mobile',$ this-> input-> post('mobile'))
$ this-> db-> set('birth_date',$ this-> input-> post('birth'))

$ this-> db-> set('first_name',$ this-> input-> post('first_name'))
$ this-> db-> set('last_name',$ this-> input-> post('last_name'));

$ this-> db-> set('city',$ this-> input-> post('city'));

$ this-> db-> set('main_street',$ this-> input-> post('main_street'));

$ this-> db-> set('sub_street',$ this-> input-> post('sub_street'))

if($ this-> input-> post('type')){
$ this-> db-> set('sex',$ this-> ; input-> post('type'));
}

if($ this-> input-> post('lat')and $ this-> input-> post('lng')){
$ this-> db-> set('location',serialize(array($ this-> input-> post('lat'),$ this-> input-> post '))));
}

if($ this-> input-> post('password')!==''){
$ this-> db-& set('password',md5($ this-> input-> post('password')));
}

$ this-> db-> where('id',$ id);
$ this-> db-> update('admins');
}

public function del($ id = FALSE){
$ this-> db-> where('id',$ id);
$ this-> db-> delete('admins');
}
}

我要添加一个允许上传图片的字段。

解决方案

上传文件时生成if语句

  if(isset($ _ FILES ['userfile'])&&!empty($ _ FILES ['userfile'] ['name'])&& $ this-> form_validation- > run()!= FALSE)
{

$ profile = array(
'emp_code'=> $ this-> input-& '),
'fname'=> $ this-> input-> post('fname'),
'mname'=> $ this-> input-> post 'mname'),
'lname'=> $ this-> input-> post('lname'),
'dob'=> date('Ym-d',strtotime ($ this-> input-> post('dob'))),
'address'=> $ this-> input-> post('address'),
' gender'=> $ this-> input-> post('gender'),
'emp_status'=> $ this-> input-> post('emp_status'),
'position'=> $ this-> input-> post('position'),
'dep_id'=> $ this-> input-> post('dep_id'),
'payroll_num'=> $ this-> input-> post('pay_num'),
'tax_code_id'=> $ this-> input-> post('tax_code'),
'tax_id'=> $ this-> input-> post('tax_id'),
'date_joined'=>日期('Y-m-d',strtotime($ this-> input-> post('doj'))),
'sss_num'=& $ this-> input-> post('sss_num'),
'PhilHealth_num'=> $ this-> input-> post('PhilHealth_num'),
'pagibig_num'=> $ this-> input-> post('pagibig_num'),
'image'=> $ info ['file_name']
);
$ id = $ this-> accounts-> insert_employee($ profile);
// insert with image
$ sched = array(
'Monday'=> $ v1,
'Tuesday'=> $ v2,
'星期三'=> $ v3,
'Thursday'=> $ v4,
'Friday'=> $ v5,
'Saturday'=> $ v6,
'Sunday'=> $ v7,
'date_From'=> $ this-> input-> post('from'),
'date_to'=> $ this-> input-> post('to'),
'emp_id'=> $ id

);
$ this-> accounts-> insert_emp_sched($ sched);
redirect('account / view_all_employee');



} else if(isset($ _ FILES ['userfile'])&& amp; empty($ _ FILES ['userfile'] ['name'])& ;& $ this-> form_validation-> run()!= FALSE)
{

$ profile = array(
'emp_code'=> $ this- > input-> post('emp_code'),
'fname'=> $ this-> input-> post('fname'),
'mname'=& this-> input-> post('mname'),
'lname'=> $ this-> input-> post('lname'),
'dob'=> ; date('Ym-d',strtotime($ this-> input-> post('dob'))),
'address'=> $ this-> input-& 'address'),
'gender'=> $ this-> input-> post('gender'),
'emp_status'=> $ this-> input-> post('emp_status'),
'position'=> $ this-> input-> post('position'),
'dep_id'=> $ this-& > post('dep_id'),
'payroll_num'=> $ this-> input-> post('pay_num'),
'tax_code_id'=> $ this-> input-> post('tax_code'),
'tax_id'=> $ this-> input-> post('tax_id'),
'date_joined'=>日期('Y-m-d',strtotime($ this-> input-> post('doj'))),
'sss_num'=& $ this-> input-> post('sss_num'),
'PhilHealth_num'=> $ this-> input-> post('PhilHealth_num'),
'pagibig_num'=> $ this-> input-> post('pagibig_num'),
);
$ id = $ this-> accounts-> insert_employee($ profile);
// insert without image
$ sched = array(
'Monday'=> $ v1,
'Tuesday'=> $ v2,
'星期三'=> $ v3,
'Thursday'=> $ v4,
'Friday'=> $ v5,
'Saturday'=> $ v6,
'Sunday'=> $ v7,
'date_From'=> $ this-> input-> post('from'),
'date_to'=> $ this-> input-> post('to'),
'emp_id'=> $ id

);
$ this-> accounts-> insert_emp_sched($ sched);
$ this-> session-> set_flashdata('item','新添加的员工数量\'没有个人资料Pic< a href =view_employee /'.$ id。'>查看用户< / a>');
redirect('account / view_all_employee');
} else {
$ data ['emp_status'] = array('Part-Time'=>'Part-Time','Regular'=>'Regular','Probationary'=> ;'Probationary');
$ data ['gender'] = array('male'=>'Male','female'=>'Female');
$ this-> load-> view('employee / create',$ data); //表单验证中的错误
}


I want to add an upload image field to register a form in CodeIgniter.

My register code in the controller:

function add_new_ticket() {

    if ($this->input->post('fullname', TRUE)
        && $this->input->post('nID', TRUE)
        && $this->input->post('age', TRUE)
        && $this->input->post('gender', TRUE)
        // && $this->input->post('isFileID', TRUE)
        // && $this->input->post('FileID', TRUE)
        && $this->input->post('phone', TRUE)
        && $this->input->post('type', TRUE)
        && $this->input->post('opsType', TRUE)
        && $this->input->post('clienc', TRUE)
        && $this->input->post('dr', TRUE)
        && strlen($this->input->post('phone', TRUE)) >= 9
        && strlen($this->input->post('nID', TRUE)) >= 9
        && (int) $this->input->post('age', TRUE) <= 99
        && count(explode('-',$this->input->post('default-date', TRUE)) > 1)
    ){

Also my code in the model file:

<?php
    class user extends CI_Model {

        public function add() {
            $data = array(
                'name' => $this->input->post('name'),
                'password' => $this->input->post('password'),
                'add_date' => time(),
                'email' => $this->input->post('email'),
                'birth_date' => $this->input->post('birth'),
                'phone' => $this->input->post('phone'),
                'mobile' => $this->input->post('mobile'),
                'sex' => $this->input->post('type'),
                'city' => $this->input->post('city'),
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'main_street' => $this->input->post('main_street'),
                'sub_street' => $this->input->post('sub_street'),
                'type' => $this->input->post('member_type'),
                'delegate_name' => $this->input->post('delegate_name'),
                'delegate_email' => $this->input->post('delegate_email'),
                'delegate_pass' => md5($this->input->post('delegate_pass')),
                'location' => serialize(array($this->input->post('lat'), $this->input->post('lng')))
            );
            $this->db->insert('admins', $data);
        }

        public function edit($id = FALSE) {

            $this->db->set('name', $this->input->post('name'));

            $this->db->set('email', $this->input->post('email'));
            $this->db->set('phone', $this->input->post('phone'));
            $this->db->set('mobile', $this->input->post('mobile'));
            $this->db->set('birth_date', $this->input->post('birth'));

            $this->db->set('first_name', $this->input->post('first_name'));
            $this->db->set('last_name', $this->input->post('last_name'));

            $this->db->set('city', $this->input->post('city'));

            $this->db->set('main_street', $this->input->post('main_street'));

            $this->db->set('sub_street', $this->input->post('sub_street'));

            if ($this->input->post('type')) {
                $this->db->set('sex', $this->input->post('type'));
            }

            if ($this->input->post('lat') and $this->input->post('lng')) {
                $this->db->set('location', serialize(array($this->input->post('lat'), $this->input->post('lng'))));
            }

            if ($this->input->post('password') !== '') {
                $this->db->set('password', md5($this->input->post('password')));
            }

            $this->db->where('id', $id);
            $this->db->update('admins');
        }

        public function del($id = FALSE) {
            $this->db->where('id', $id);
            $this->db->delete('admins');
        }
    }

I want to add a field that allows to upload images.

解决方案

make an if statement when uploading bro

if(isset($_FILES['userfile']) && !empty($_FILES['userfile']['name']) && $this->form_validation->run() != FALSE)
            {

                $profile = array(
                    'emp_code' => $this->input->post('emp_code'), 
                    'fname' => $this->input->post('fname'), 
                    'mname' => $this->input->post('mname'), 
                    'lname' => $this->input->post('lname'),
                    'dob' => date('Y-m-d',strtotime($this->input->post('dob'))),
                    'address' => $this->input->post('address'),
                    'gender' => $this->input->post('gender'),
                    'emp_status' => $this->input->post('emp_status'),
                    'position' => $this->input->post('position'),
                    'dep_id' => $this->input->post('dep_id'),
                    'payroll_num' => $this->input->post('pay_num'),
                    'tax_code_id' => $this->input->post('tax_code'), 
                    'tax_id' => $this->input->post('tax_id'), 
                    'date_joined' => date('Y-m-d',strtotime($this->input->post('doj'))),
                    'sss_num' => $this->input->post('sss_num'),
                    'PhilHealth_num' => $this->input->post('PhilHealth_num'),
                    'pagibig_num' => $this->input->post('pagibig_num'),
                    'image'=> $info['file_name']
                    );
                $id = $this->accounts->insert_employee($profile);
//insert with image
                $sched = array(
                    'Monday'=>$v1,
                    'Tuesday'=>$v2,
                    'Wednesday'=>$v3,
                    'Thursday'=>$v4,
                    'Friday'=>$v5,
                    'Saturday'=>$v6,
                    'Sunday'=>$v7,
                    'date_From'=>$this->input->post('from'),
                    'date_to'=>$this->input->post('to'),
                    'emp_id'=> $id

                    );
                $this->accounts->insert_emp_sched($sched);
                redirect('account/view_all_employee');



            }else if(isset($_FILES['userfile']) && empty($_FILES['userfile']['name']) && $this->form_validation->run() != FALSE)
            {

                $profile = array(
                    'emp_code' => $this->input->post('emp_code'), 
                    'fname' => $this->input->post('fname'), 
                    'mname' => $this->input->post('mname'), 
                    'lname' => $this->input->post('lname'),
                    'dob' => date('Y-m-d',strtotime($this->input->post('dob'))),
                    'address' => $this->input->post('address'),
                    'gender' => $this->input->post('gender'),
                    'emp_status' => $this->input->post('emp_status'),
                    'position' => $this->input->post('position'),
                    'dep_id' => $this->input->post('dep_id'),
                    'payroll_num' => $this->input->post('pay_num'),
                    'tax_code_id' => $this->input->post('tax_code'), 
                    'tax_id' => $this->input->post('tax_id'), 
                    'date_joined' => date('Y-m-d',strtotime($this->input->post('doj'))),
                    'sss_num' => $this->input->post('sss_num'),
                    'PhilHealth_num' => $this->input->post('PhilHealth_num'),
                    'pagibig_num' => $this->input->post('pagibig_num'),
                    );
                $id = $this->accounts->insert_employee($profile);
//insert without image
                $sched = array(
                    'Monday'=>$v1,
                    'Tuesday'=>$v2,
                    'Wednesday'=>$v3,
                    'Thursday'=>$v4,
                    'Friday'=>$v5,
                    'Saturday'=>$v6,
                    'Sunday'=>$v7,
                    'date_From'=>$this->input->post('from'),
                    'date_to'=>$this->input->post('to'),
                    'emp_id'=> $id

                    );
                $this->accounts->insert_emp_sched($sched);
                $this->session->set_flashdata('item','The Newly added Employee Dose\'nt have a Profile Pic <a href="view_employee/'.$id.'">View The User</a>');
                redirect('account/view_all_employee');
            }else{
                $data['emp_status'] =  array('Part-Time' => 'Part-Time','Regular' => 'Regular', 'Probationary' => 'Probationary' );
                $data['gender'] = array('male' => 'Male','female'=>'Female' );
                $this->load->view('employee/create',$data); //error in form validation
            }

这篇关于使用CodeIgniter添加上传图像字段以注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆