image upload and save path in database using codeigniter

前端 未结 2 727
一个人的身影
一个人的身影 2021-01-27 21:54

i did not get image upload and save image path in database My table name is uploadimage and controller name is upload.php

         


        
2条回答
  •  时光取名叫无心
    2021-01-27 22:44

    Hi you need to have this

    view.php

    
        
    
    

    upload.php

    function do_upload(){
        $name = $_FILES["file"]["name"];
        $ext = end((explode(".", $name))); # extra () to prevent notice
    
        $config['upload_path']   = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']      = 0;
    
        $this->load->library('upload', $config);
    
        if( ! $this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors());
    
            $this->load->view('upload_form', $error);
        }
        else{
            $upload_data = $this->upload->data();
    
            #you can choose from
            /*
               Array(
                     [file_name]    => mypic.jpg
                     [file_type]    => image/jpeg
                     [file_path]    => /path/to/your/upload/
                     [full_path]    => /path/to/your/upload/jpg.jpg
                     [raw_name]     => mypic
                     [orig_name]    => mypic.jpg
                     [client_name]  => mypic.jpg
                     [file_ext]     => .jpg
                     [file_size]    => 22.2
                     [is_image]     => 1
                     [image_width]  => 800
                     [image_height] => 600
                     [image_type]   => jpeg
                     [image_size_str] => width="800" height="200"
              )
            */
    
            $this->model->insert_data($upoad_data['file_name'], $upoad_data['full_path']);
    
            $data = array('upload_data' => $this->upload->data());
    
            $this->load->view('upload_success', $data);
        }
    }
    

    model.php

    function insert_data($name, $path_name){
        $data = array(
                      'name'    => $name,
                      'path'    => $path_name
                     );
    
        $this->db->insert('table', $data);
    
        return $this->db->insert_id();
    }
    

    With that, you upload your file, then get the data about the upload and insert it in db

提交回复
热议问题