上传数据库中的Image目录

时间:2015-05-16 08:31:52

标签: php codeigniter

我想在单击提交按钮时将图像目录与其他数据一起上传到数据库中我希望它能够将所有数据上传到数据库中。 我想将图像与学生详细信息一起上传以下是上传控制器功能在管理文件中的文件结构。

`控制器。

   function add_student(){

    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

    $this->form_validation->set_rules('fname', 'Student Firstname', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('mname', 'Student Middlename', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('lname', 'Student Lastname', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('gender', 'Choose The Gender', 'required|min_length[2]|max_length[35]');

    $this->form_validation->set_rules('datepicker', 'Please pick a DOB', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('marital', 'Please Enter Marital Status', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('pob', 'Please Enter POB Residence', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('pobdistrict', 'Please Enter The POB District', 'required|min_length[2]|max_length[15]');

    $this->form_validation->set_rules('pobregion', 'Please Enter The POB Region', 'required|min_length[5]|max_length[15]');


    $this->form_validation->set_rules('residence', 'Please Enter The Residence', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('region', 'Please Enter The Region', 'required|min_length[5]|max_length[15]');

    $this->form_validation->set_rules('district', 'Please Enter The District', 'required|min_length[5]|max_length[15]');


    $this->form_validation->set_rules('education', 'Please Enter The Education Level', 'required|min_length[5]|max_length[30]');

    $this->form_validation->set_rules('phone', 'Please Enter The Education Level', 'required|min_length[5]|max_length[15]');




    if ($this->form_validation->run() == FALSE)
    {
        $this->load->model('fetch_data');

        $data['schoolname'] =$this->fetch_data->school_data();

        $data['partnername'] =$this->fetch_data->partner_data();

        $data['main_content'] = '/student/addstudent';

        $this->load->view('includes/template',$data);

    }else{
        $config['upload_path'] = '../images/student_profile';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = "2048000"; // Can be set to particular file size , here it is 2 MB(2048 Kb)
        $config['max_height'] = "768";
        $config['max_width'] ="1024";
        $this->load->library('upload', $config);

        if(!$this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors());

            echo $error;

        }
        $data_upload_files = $this->upload->data();

        $image = $data_upload_files['full_path'];



        $data = array(
            'student_fname' => $this->input->post('fname'),
            'student_mname' => $this->input->post('mname'),
            'student_lname' => $this->input->post('lname'),
            'gender' => $this->input->post('gender'),
            'y_enrolled' => $this->input->post('datepicker'),
            'marital_status' => $this->input->post('marital'),
            'education' => $this->input->post('education'),

            'pob' => $this->input->post('pob'),
            'region' => $this->input->post('pobregion'),
            'district' => $this->input->post('pobdistrict'),

            'residence' => $this->input->post('residence'),
            'res_district' => $this->input->post('district'),

            'res_region' => $this->input->post('region'),
            'phone' => $this->input->post('phone'),

            'student_profile_img' => $image ,
        );


        // Transfering Data To Model

        $this->load->model('insert_data');

        $this->insert_data->student_data($data);

    }


}`


**The model**


` 
 public function student_data($data){

    $this->db->insert('student_profile', $data);
}`

1 个答案:

答案 0 :(得分:0)

您真的想在数据库中插入BINARY数据吗?相信我,你不会。你只需要保存文件的路径!

如果您真的想保存它,请在base64中转换图像,然后将其写入数据库。

$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);