Tutorial Upload Validation Unlink File, For Edit To Database With CodeIgniter


kali ini saya aan berbagi tentang bagaimana cara membuat upload validasi pada codeigniter dan masuk ke database, untuk itu langsung saja :

buat database nya bebas, saya asumsikan anda sudah bisa setting database nya ya, kemudian buatlah tabel seperti berikut :



setelah itu buatlah controller dengan nama file nya yaitu top-menu.php, setting_nota.php

top-menu.php

<html>
<head>
  <title>Admin Area</title>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css' type='text/css' media='all' />
  <link rel='stylesheet' href='https://fortawesome.github.io/Font-Awesome/assets/css/site.css' type='text/css' media='all' />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>

  <?php echo "<link rel='icon' href='".base_url()."/assets/uploads/logo/".$LoNota."'>"; ?>

  <style>
      ::selection { background-color: #E13300; color: white; }
    ::-moz-selection { background-color: #E13300; color: white; }
    #button-x-mass {
      background-color:#D91E18;
      color:#fff;
      border: none;
      width:50%;
    }
    #button-x-mass:hover {
      background-color:#CF000F;
      color:#fff;
    }
    #button-menu {
      background-color:#03A678;
      color:#fff;
      border:none;
      border-radius:20px;
      border: none;
    }
    #button-menu:hover {
      background-color:#16a085;
      color:#fff;
    }   
    #button-x-single {
      background-color:#22A7F0;
      color:#fff;
            border-radius:20px;
      border: none;
    }
    #button-x-single:hover {
      background-color:#3498DB;
      color:#fff;
      border:none;
    }
    #text-form {
      font-weight: bold;
      color: #1F3A93;
    }
    #text-form:hover {
      font-weight: bold;
      color: #1F3A93;
      -moz-box-shadow: 0px 0px 5px #000;-webkit-box-shadow: 0px 0px 5px #000; box-shadow: 0px 0px 5px #000; 
    } 
  </style>

</head>

<div class="navbar navbar-inverse navbar-static-top hidden-print">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <i class="fa fa-bars fa-lg"></i>
      </button>
      <a class="navbar-brand" href="../"><i class="fas fas-logo"></i></a>
    </div>

    <div class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li class="hidden-sm "><a href="<?php echo base_url();?>index.php/admin/dashboard">Home</a></li>

        <li class="dropdown-split-left"><a href="#">Menu Setting</a></li>
        <li class="dropdown dropdown-split-right">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">
            <i class="fa fa-caret-down"></i>
          </a>
          <ul class="dropdown-menu pull-right">

            <li><a href="<?php echo base_url();?>index.php/admin/dashboard/Setting_Nota"><i class="fa fa-diamond fa-fw"></i>&nbsp; Setting Nota</a></li>
            <li><a href="../icons/#new"><i class="fa fa-user fa-fw"></i>&nbsp; Setting Akun</a></li>
          </ul>
        </li>

        <li class="dropdown-split-left"><a href="#">Menu Nota</a></li>
        <li class="dropdown dropdown-split-right">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">
            <i class="fa fa-caret-down"></i>
          </a>
          <ul class="dropdown-menu pull-right">

            <li><a href="../icons/#new"><i class="fa fa-sticky-note-o fa-fw"></i>&nbsp; Nota Bukti</a></li>
            <li><a href="../icons/#new"><i class="fa fa-sticky-note fa-fw"></i>&nbsp; Nota Transaksi</a></li>
          </ul>
        </li> 
      <li class="hidden-sm "><a href=""><i class="fa fa-money"></i>&nbsp; Menu Report</a></li>      
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><i class="fa fa-user"></i>&nbsp; Login As <?php echo $username; ?></a></li>
        <li><a href="<?php echo site_url('admin/dashboard/logout'); ?>"><i class="fa fa-sign-out"></i>&nbsp; Logout</a></li>
      </ul>
    </div>
  </div>
</div>

<body style="background:#EEEEEE;"><br><br>

Kemudian buat File setting_nota.php simpan di view

<style>
 .fileUpload { position: relative; overflow: hidden; margin:0; } 
 .fileUpload input.upload { position: absolute; top: 0; right: 0; margin: 0; padding: 0; font-size: 20px; cursor: pointer; opacity: 0; filter: alpha(opacity=0); }
</style>

 <div class="container">
  <div class="row">
   <div class="col-md-12 col-sm-12">
    <h2><i class="fa fa-cog fa-fw"></i> Update Nama Nota</h2><br>
    <?php echo "<img src='".base_url()."/assets/uploads/logo/".$LoNota."' style='width:10%;'>"; ?><br><br>
    <?php echo form_open_multipart('admin/dashboard/Setting_Nota_Update'); ?>
    <input type="hidden" name="id" value="<?php echo $IDNota; ?>" id="text-form" class="form-control" style="width:30%;" placeholder="Id Setting" aria-describedby="basic-addon1">
    <input type="text" name="nama_nota" value="<?php echo $NmNota; ?>" id="text-form" class="form-control" style="width:30%;" placeholder="Nama Nota" aria-describedby="basic-addon1"><br>
    <input type="text" name="kontak" value="<?php echo $CpNota; ?>" id="text-form" class="form-control" style="width:30%;" placeholder="Kontak" aria-describedby="basic-addon1"><br>
    <textarea name="alamat" class="form-control" id="text-form" aria-describedby="basic-addon1" style="width:50%;"><?php echo $AlNota; ?></textarea><br>
    
    <input id="uploadFile"   placeholder="Pilih File..." disabled="disabled" style="width:43%;" />
    <div class="fileUpload btn btn-primary" id="button-x-mass" style="width:7%;">
        <span>Upload</span>
        <input id="uploadBtn" type="file" name="logo" class="upload" />
    </div>
    <br><br>
    <button class="btn btn-default" id="button-x-mass">Update</button>
    </form>
    <h3 class="info"><?php echo $this->session->flashdata('message'); ?></h3>
   </div>
  </div>
 </div>


<script>

document.getElementById("uploadBtn").onchange = function () {
    document.getElementById("uploadFile").value = this.value;
};
</script>

buat modelnya setting_nota.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Setting_nota extends CI_Model {

    public function Update_Nota($Table, $data, $where) {
        $res = $this->db->update($Table, $data, $where);
        return $res;
    }
} 

Lalu Buat File dashboard.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends CI_Controller {

 public function __construct() {
  parent::__construct();

  $this->load->library('session');
  if($this->session->userdata('username')=="") {
   redirect('login_user/loginpage');
  }

  $this->load->model('user/model_user');
  $this->load->helper(array('form','url','text','file'));
  $this->load->library('form_validation');
 }

 public function index() {

  $data['username']  = $this->session->userdata('username');
  $data['NmNota']  = $this->NamaNota().$this->Nama_Nota;
  $data['AlNota']  = $this->NamaNota().$this->Alamat_Nota;
  $data['CpNota']  = $this->NamaNota().$this->Kontak_Nota;
  $data['LoNota']  = $this->NamaNota().$this->Logo_Nota;

  $this->load->view('admin/top-menu', $data);
  $this->load->view('admin/index',$data);
  //$this->load->view('admin/footer');
 } 

 private function NamaNota() {
  $data = $this->model_user->ShowNamaToko();
  foreach($data as $d) {
   $this->Id_Nota   = $d['id'];
   $this->Nama_Nota  = $d['nama_nota'];
   $this->Alamat_Nota = $d['alamat'];
   $this->Kontak_Nota = $d['kontak'];
   $this->Logo_Nota = $d['logo'];
  }
 }

 public function Setting_Nota() {
  $data['username']  = $this->session->userdata('username');
  $data['IDNota']  = $this->NamaNota().$this->Id_Nota;
  $data['NmNota']  = $this->NamaNota().$this->Nama_Nota;
  $data['AlNota']  = $this->NamaNota().$this->Alamat_Nota;
  $data['CpNota']  = $this->NamaNota().$this->Kontak_Nota;
  $data['LoNota']  = $this->NamaNota().$this->Logo_Nota;

  $this->load->view('admin/top-menu', $data);
  $this->load->view('admin/setting_nota', $data, array('error' => ' ', ));
 }

 public function Setting_Nota_Update() {

  $config['upload_path']   = 'assets/uploads/logo/';
  $config['allowed_types']  = 'gif|jpg|png';
  $config['max_size']   = '1000';
  $config['max_width']    = '1024';
  $config['max_height']    = '768';
  $config['encrypt_name']  = TRUE;
  
  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload('logo')) {

   $this->load->model('user/setting_nota');
   $data = $this->setting_nota->Update_Nota('setting', array(
    "nama_nota"  => $this->input->post('nama_nota'),
    "alamat"   => $this->input->post('alamat'),
    "kontak"   => $this->input->post('kontak'),
   ), array('id' => $this->input->post('id') ));

   $this->session->set_flashdata('message',  $this->upload->display_errors()."Logo Tidak Diubah...!!!");
   redirect('admin/dashboard/Setting_Nota');

  } else {

   $showLogo = $this->model_user->ShowNamaToko();
   foreach($showLogo as $d) {
    $Logo_Nota = $d['logo'];
    unlink('assets/uploads/logo/'.$Logo_Nota);
   }
   
   $this->load->model('user/setting_nota');
   $data = $this->setting_nota->Update_Nota('setting', array(
    "nama_nota"  => $this->input->post('nama_nota'),
    "alamat"   => $this->input->post('alamat'),
    "kontak"   => $this->input->post('kontak'),
    "logo"    => $this->upload->file_name,
   ), array('id' => $this->input->post('id') ));
   if($data >= 1) {
    $this->session->set_flashdata('message', 'Berhasil Update Setting Nota...!!!<br><br>');
    redirect('admin/dashboard/Setting_Nota');
   } else {
    $this->session->set_flashdata('message', 'Gagal Update Setting Nota...!!!<br><br>');
    redirect('admin/dashboard/Setting_Nota');
   }
  }
 }

}

Subscribe to receive free email updates: