如何使用Codeigniter将数据上传到数据库

时间:2019-04-12 07:08:55

标签: php database codeigniter

我想使用codeigniter将图像上传到数据库。它保存到数据库,但是图像没有发送到我设置的文件夹路径,只有image_path保存到数据库。你能告诉我怎么做或我做错了什么吗?这是我的代码:

*控制器*


function saveEvent(){

        $config = [
            'upload_path' => './assets/img',
            'allowed_types' => 'gif|png|jpg|jppeg'
        ];

        $this->load->library('upload', $config);
        $this->form_validation->set_error_delimiters();
        $this->upload->do_upload();
        // var_dump('asd'. $this->upload->do_upload());
        // if($this->upload->do_upload()){
            $datas = $this->input->post('eventimage');
            $info = $this->upload->data();
            $image_path = "./assets/img/".$info['raw_name'].$info['file_ext'];
            $data['cal_events_image'] = $image_path;
            $data['cal_events_name'] = $this->input->post('eventname');
            unset($data['submit']);
            $this->load->model('CalendarModel');
            if($this->CalendarModel->eventinsertdata($data)){
                echo "success";
            }else{
                echo " fail";
            }

*查看*


<form action="<?php echo base_url('index.php/CalendarController/saveEvent');?>" enctype="multipart/form-data" method="post" accept-charset="utf-8">
                <table class="table">
                    <tr>
                        <td>Event Type</td>
                        <td><input type="text" name="eventname"></td> 
                    </tr>
                    <tr>
                        <td>Image</td>
                        <td><input type="file" name="eventimage"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="submitevent" value="Add Event" class="btn btn-success"></td>
                    </tr>
                </table>
            </form>

*型号*

function eventinsertdata($data){
return $this->db->insert('table', $data);
     return $this->db->insert('dev_adkt_events_type', $data);
        echo 'hahay';

    }

先谢谢大家。希望你能帮我这个忙。

1 个答案:

答案 0 :(得分:1)

我已经解决了!这是我的答案,它有效!

控制器


function saveEvent(){
        $config = array(
            'upload_path' => './assets/img',
            'allowed_types' => 'gif|png|jpg|jppeg'
        );

        get_instance()->load->library('upload', $this->config); 
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->form_validation->set_error_delimiters();
        if($this->upload->do_upload('eventimage')){
            $data = $this->input->post('eventimage');
            $info = $this->upload->data();
            // var_dump($info);
            $image_path = "/assets/img/".$info['raw_name'].$info['file_ext'];
            $datas['cal_events_image'] = $image_path;
            $datas['cal_events_name'] = $this->input->post('eventname');
            unset($data['submit']);
            $this->load->model('CalendarModel');
            if($this->CalendarModel->eventinsertdata($datas)){
                // echo "success";
                redirect($_SERVER['HTTP_REFERER']);
            }else{
                echo $this->upload->display_errors(); 
            }
        }else{
            // echo 'shit';
            echo $this->upload->display_errors(); 
        }
    }