代码点火器 - 如何在我的数据库mysql / php中保存图像

时间:2014-02-05 07:42:53

标签: php mysql codeigniter

在我的页面中,我创建了一个包含姓名,电子邮件,密码等不同字段的表单,现在我必须上传图像并存储在我的数据库中。任何人都发送包含图像上传文件在内的所有字段的代码。我必须多次尝试,图像无法上传到我的数据库中。 我的控制器是

function activity()
    {
        $this->load->library('form_validation');
        //field name,error message, validation rules
        $this->form_validation->set_rules('activityid', 'Activity Id', 'trim|required');
        $this->form_validation->set_rules('hostid', 'Host Id', 'trim|required');
        $this->form_validation->set_rules('activityname', 'Activity Name', 'trim|required');
        $this->form_validation->set_rules('date', 'Date', 'trim|required');
        $this->form_validation->set_rules('venue', 'Venue', 'trim|required');
        $this->form_validation->set_rules('typeofactivity', 'Type of Activity', 'trim|required');
        $this->form_validation->set_rules('conductedby', 'Conducted By', 'trim|required');
         $config['upload_path'] = './asset/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);    
$w = $this->upload->data();
        if($this->form_validation->run()==FALSE)
        {
            //$this->load->view('signup_form');
            $this->activity_reg();
        }
        else
        {
             $this->load->model('membership_model');

            $query=$this->membership_model->activity();

            if($query)
            {

                $data['main_content']='signup_successful';
                $this->load->view('includes/templates',$data);
            }
            else
            {
                $this->load->view('activity');
            }
        }
    }

我的模特是

function activity()

    {
        $w = $this->upload->data();
        $new_member_insert_data= array(

                        'activityid'=>$this->input->post('activityid'),
                        'hostid'=>$this->input->post('hostid'),
                        'activityname'=>$this->input->post('activityname'),
                        'date'=>$this->input->post('date'),
                        'venue'=>$this->input->post('venue'),
                        'typeofactivity'=>$this->input->post('typeofactivity'),
                        'conductedby'=>$this->input->post('conductedby'),
                            'image' => $w['file_name']
            );

                        $this->load->database();
                        $insert=$this->db->insert('activity',$new_member_insert_data);
                        return $insert;

    }

1 个答案:

答案 0 :(得分:1)

使用file_get_contents()函数。

$image = $_FILES['YOUR_INPUT_NAME']['tmp_name'];
$new_member_insert_data= array(
                    'activityid'=>$this->input->post('activityid'),
                    'hostid'=>$this->input->post('hostid'),
                    'activityname'=>$this->input->post('activityname'),
                    'date'=>$this->input->post('date'),
                    'venue'=>$this->input->post('venue'),
                    'typeofactivity'=>$this->input->post('typeofactivity'),
                    'conductedby'=>$this->input->post('conductedby'),
                    'image' => file_get_contents( $image )
        );