如何在我的数据库中保存图像?

0 php 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');
    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');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的模特是

function activity() {
    $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')
    );

    $this->load->database();
    $insert = $this->db->insert('activity', $new_member_insert_data);
    return $insert;
}
Run Code Online (Sandbox Code Playgroud)

Ala*_*all 5

通常最好将图像存储在文件系统中,并将数据保存PATH为文本中的图像.

如果您在使用数据库时已经死定,则需要将图像插入为 BLOB.

这里有一个很好的概述和教程:http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html#3