title: how to upload image in codeigniter 3
body: <p>i have trouble with uploading an image using codeigniter. i am new in this and didin't have a clue in how to upload image using codeigniter. so i hope any of you guys can help me out</p>
  
  <p>this is my form named form_add.php</p>
  
  <pre><code>&lt;form method="POST" action="&lt;?php echo base_url()."index.php/crud/insert"; ?       &gt;"&gt;
      &lt;table style="width:110%" class="table table-striped table table-bordered table table-hover"&gt;
  
  
              &lt;td&gt;Nama&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="nama_produk"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Info&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="info_produk"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Harga&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="harga_produk"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Stock&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="stock"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
  
      &lt;?php echo form_open_multipart('upload/do_upload');?&gt;
          &lt;tr&gt;
              &lt;td&gt;Gambar&lt;/td&gt;
              &lt;td&gt;&lt;input type="file" name="gambar_produk"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Di Buat Oleh&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="penulis_produk"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Kategori&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="kategori"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Kode Kategori&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="kode_kategori"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="submit" name="btnsubmit" value="Simpan" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>and this is my controller named crud.php</p>
  
  <pre><code>&lt;?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  
  class Crud extends CI_Controller {
  public function __construct()
      {
              parent::__construct();
              $this-&gt;load-&gt;helper(array('form', 'url'));
      }
  
  public function index(){
      $data = $this-&gt;mymodel-&gt;GetAll();
  
      $this-&gt;load-&gt;view('view_all_produk',array('data' =&gt; $data));
  
  }
  
  public function add_data(){
      $this-&gt;load-&gt;view('form_add');
  
  }
  
  
  public function insert(){
      $nama_produk = $_POST['nama_produk'];
      $info_produk = $_POST['info_produk'];
      $harga_produk = $_POST['harga_produk'];
      $stock = $_POST['stock'];
      $penulis_produk = $_POST['penulis_produk'];
      $gambar_produk = $_FILES['gambar_produk']['name'];
      $gambar_tmp = $_FILES['gambar_produk']['tmp_name'];
      $kategori = $_POST['kategori'];
      $kode_kategori = $_POST['kode_kategori'];
      $data_insert = array(
          'nama_produk' =&gt; $nama_produk,
          'info_produk' =&gt; $info_produk,
          'harga_produk' =&gt; $harga_produk,
          'stock' =&gt; $stock,
          'penulis_produk' =&gt; $penulis_produk,
          'gambar_produk' =&gt; $gambar_produk,
          'kategori' =&gt; $kategori,
          'kode_kategori' =&gt; $kode_kategori
      );
      $res = $this-&gt;mymodel-&gt;insertData('produk',$data_insert);
      if($res &gt;= 1){
          $this-&gt;session-&gt;set_flashdata('pesan','Tambah Data Sukses');
          redirect('crud/index');
      }else{
          echo "&lt;h2&gt;Insert Data Gagal!!!&lt;/h2&gt;";
  
      }
  
      }
  
      public function do_upload()
      {
              $config['upload_path']          = './assets/images';
              $config['allowed_types']        = 'gif|jpg|png';
              $config['max_size']             = 300;
              $config['max_width']            = 1024;
              $config['max_height']           = 768;
  
              $this-&gt;load-&gt;library('upload', $config);
  
              if ( ! $this-&gt;upload-&gt;do_upload('gambar_produk'))
              {
                      $error = array('error' =&gt; $this-&gt;upload-  &gt;display_errors());
  
                      $this-&gt;load-&gt;view('form_add', $error);
              }
              else
              {
                      $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
  
  
              }
      }
  </code></pre>
  
  <p>and this is the model named mymodel.php</p>
  
  <pre><code>&lt;?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  
  class Mymodel extends CI_Model {
  public function GetAll($where="") {
  
      $data = $this-&gt;db-&gt;query('select * from produk '.$where);
      return $data -&gt; result_array();
  
  
  }
  
  public function insertData($tableName,$data){
          $res = $this-&gt;db-&gt;insert($tableName,$data);
          return $res;
  
  }
  
      public function updateData($tableName,$data,$where){
          $res = $this-&gt;db-&gt;update($tableName,$data,$where);
          return $res;
  
  }
  public function GetKategori($where=""){
      $data = $this-&gt;db-&gt;query('select * from kategori '.$where);
      return $data;
  }
  }
  </code></pre>
  
  <p>also this is what it shows after i run it</p>
  
  <p>A PHP Error was encountered</p>
  
  <p>Severity: Notice</p>
  
  <p>Message: Undefined index: gambar_produk</p>
  
  <p>Filename: controllers/crud.php</p>
  
  <p>Line Number: 30</p>
  
  <p>Backtrace:</p>
  
  <p>File: C:\xampp\htdocs\genstore\admin\application\controllers\crud.php
  Line: 30
  Function: _error_handler</p>
  
  <p>File: C:\xampp\htdocs\genstore\admin\index.php
  Line: 315
  Function: require_once</p>
  
  <hr>
  
  <p>A PHP Error was encountered</p>
  
  <p>Severity: Notice</p>
  
  <p>Message: Undefined index: gambar_produk</p>
  
  <p>Filename: controllers/crud.php</p>
  
  <p>Line Number: 31</p>
  
  <p>Backtrace:</p>
  
  <p>File: C:\xampp\htdocs\genstore\admin\application\controllers\crud.php
  Line: 31
  Function: _error_handler</p>
  
  <p>File: C:\xampp\htdocs\genstore\admin\index.php
  Line: 315
  Function: require_once</p>
  
  <hr>
  
  <p>A Database Error Occurred</p>
  
  <p>Error Number: 1048</p>
  
  <p>Column 'gambar_produk' cannot be null</p>
  
  <p>INSERT INTO <code>produk</code> (<code>nama_produk</code>, <code>info_produk</code>, <code>harga_produk</code>, <code>stock</code>, <code>penulis_produk</code>, <code>gambar_produk</code>, <code>kategori</code>, <code>kode_kategori</code>) VALUES ('sada', 'asdas', 'asdasd', 'sdasd', 'sdasd', NULL, 'sadasd', '14')</p>
  
  <p>Filename: C:/xampp/htdocs/genstore/admin/system/database/DB_driver.php</p>
  
  <p>Line Number: 691</p>
  
  <p>so can anyone tell me what's my mistake? thanks</p>