Home > Enterprise >  i want to update data image using codeigniter
i want to update data image using codeigniter

Time:06-15

Severity: Notice

Message: Trying to get property 'gambar' of non-object

Filename: admin/CarouselController.php

Line Number: 42

Controller

    public function edit_carousel(){//update carousel
    $id= $this->input->post('id_carousel');
    $data = $this->modelcarousel->getDataById($id)->row();
    $gambar = './assets/foto/carousel/'.$data->gambar;

    if (is_readable($gambar) && unlink($gambar)) {
        $config['upload_path']          = './assets/foto/carousel';
        $config['allowed_types']        = 'gif|jpg|png|jpeg';
        $config['max_size']             = 2048;
        $config['max_width']            = 10000000;
        $config['max_height']           = 10000000;

      $this->load->library('upload', $config);
      $this->upload->initialize($config);  
      if (!$this->upload->do_upload('gambar')) {
          $error = array('error' => $this->upload->display_errors());
      }else{
        $gambar = $this->upload->data();

        $data = array(
                'gambar' => $gambar,
                'headline' => $this->input->post('headline'),
                'deskripsi' => $this->input->post('deskripsi'),
                'status' => $this->input->post('status'),
                'tanggal_post' => $this->input->post('tanggal_post')
            );
        $update = $this->modelcarousel->update_carousel($id,$data);
        if ($update) {
            redirect('carousel');
        }else{
            echo 'Gagal';
        }
      }
    }
}

Model

function update_carousel($id,$data){//update carausel
    $this->db->where('id_carousel',$id);
    return $this->db->update('tb_carousel',$data);
}
function getDataById($id){
    $this->db->where('id_carousel',$id);
    return $this->db->get('tb_carousel');
}

database: id_carousel gambar headline deskripsi status tanggal_post

CodePudding user response:

You can try with this

 if (is_readable($gambar)) {
            $media_files = $gamba;
            $this->load->library('upload');

            // Get file data
            $type = explode("/", $gamba['type']);
            $cMediaType = $type[0];
            $cMediaName = $gamba['name'];
}

Your controller Code

CodePudding user response:

Change this line of code:

$this->load->library('upload', $config);

To this (do not send the second parameter)

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

  • Related