简体   繁体   English

在数据库中上传图片目录

[英]Upload Image directory in the database

I want to upload image directory together with other data into the database when a submit button is clicked i want it to be able to upload all the data into a database. 单击提交按钮时,我想将图像目录和其他数据一起上传到数据库中,我希望它能够将所有数据上传到数据库中。 I want to upload the image together with student details The following is the structure of my files the upload controller function is in Admin files. 我想将图像和学生详细信息一起上传以下是我的文件结构,上传控制器功能位于Admin文件中。

`The controller. `控制器。

   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);
}`

Do you REALLY wants to insert BINARY data in your database? 您是否真的要在数据库中插入BINARY数据? Trust me, you won't. 相信我,你不会。 You just have to save the path to the file! 您只需要保存文件的路径即可!

If you really want to save it, transform your image in base64 then write it in your database. 如果您真的要保存它,请在base64中转换图像,然后将其写入数据库中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM