使用 CodeIgniter 将上传图像字段添加到注册表单

Posted

技术标签:

【中文标题】使用 CodeIgniter 将上传图像字段添加到注册表单【英文标题】:Add upload image field to register form using CodeIgniter 【发布时间】:2016-01-16 14:49:01 【问题描述】:

我想在CodeIgniter中添加一个上传图片字段来注册一个表单。

我在控制器中的注册码:

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)
    )

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

<?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');
        
    

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

【问题讨论】:

你看过codeigniter文件上传codeigniter.com/user_guide/libraries/file_uploading.html 是的,我做到了,但你不明白我想将此选项插入注册表单中 你到底想要什么你想知道如何上传图片吗?? 【参考方案1】:

我假设您想在表单中添加“图片上传”选项

public function add() 



/* Configuration array various settings

  can be set here

  **/
 $config['upload_path'] = '/file_path/'; // path where image will be saved
         $config['allowed_types'] = 'gif|jpg|png|jpeg';
         $this->load->library('upload', $config);
         $this->upload->do_upload('image');
         $data_upload_files = $this->upload->data();

         $image = $data_upload_files[full_path];
$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')))
        'image'=>$image; // add this for image

    );
 $this->db->insert('admins', $data);


View 有这样的东西

<input type="file" name="image" size="20" />

控制器

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
        && $this->upload->do_upload() // add something like this              
&& count(explode('-',$this->input->post('default-date', TRUE)) > 1)
            )

注意:

输入名称必须在$this->upload->do_upload('image')中定义相同;即名称="图像"

输入元素必须有多个=“多个”或只有多个

3.$this->load->library('upload'); //加载库

4.回调,$this->upload->do_upload() 会将给定字段名称中选择的文件上传到目标文件夹。

5.回调 $this->upload->data() 返回一个与上传文件相关的数据数组,如文件名、路径、大小等。

【讨论】:

谢谢你帮助我,但我希望你能简单地解释一下,因为我不明白,我应该把哪些代码放在(控制器、模型、视图)文件夹中 你好,我尝试这样做,但没有图像上传我的 sql 数据库中是否有任何修改,例如添加表或做任何事情 我没有看到任何错误,但我在“上传”目录中看不到图像,尽管我获得了 777 权限 @ahmed 在模型中的 $this-&gt;db-&gt;insert('admins', $data); 之前添加 die(var_dump($data)); 并显示结果 我这样做了,但是当我填写了所有信息然后按下按钮时,它似乎冻结没有任何变化【参考方案2】:

上传的时候做if语句

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 将上传图像字段添加到注册表单的主要内容,如果未能解决你的问题,请参考以下文章

带有 Summernote 的 CodeIgniter - 无法将图像上传到数据库

无法将变量从控制器传递到模型以处理codeigniter中的多个上载字段

使用 CodeIgniter 上传多张图片

CodeIgniter将一个图像上传到两个目录

如何将图像路径和名称上传到数据库 - Codeigniter

如何将文件图像存储到 mysql (Codeigniter)