Membuat file upload yang lama terhapus dengan yang baru

Permisi teman-teman, ingin bertanya..
Cara agar membuat file upload yang lama bisa terganti dengan file upload yang baru, kemarin saya nyoba2 pakai unlink tapi belum bisa, mungkin ada yang bisa membantu..

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

class Editdata extends CI_Controller{

	function __construct()
	{
		parent::__construct();
		$this->load->model('M_data','model');
		$this->load->helper('url','download','file');
		$this->load->database();
		$this->table = 'tb_data';

	}

	function index()
	{
		$this->load->view('pegawai/editdata');
	}

	function edit()
	{
		if(isset($_POST['edit']))
		{

			$id = $this->input->post('id');
			$tahunpkpt = $this->input->post('tahunpkpt');
			$tanggal = $this->input->post('tanggal');
			$tanggal = date('Y-m-d', strtotime($tanggal));
			$lhp = $this->input->post('lhp');
			$temuan = $this->input->post('temuan');
			$rekomendasi = $this->input->post('rekomendasi');
			$tindaklanjut = $this->input->post('tindaklanjut');
			$entrydokumen = $this->input->post('entrydokumen');
			$feedback = $this->input->post('feedback');
			$datapendukung = $this->upload->data('file_name');


			$data = array(
				'tahunpkpt'=>$tahunpkpt,
				'tanggal'=>$tanggal,
				'lhp'=>$lhp,
				'temuan'=>$temuan,
				'rekomendasi'=>$rekomendasi,
				'tindaklanjut'=>$tindaklanjut,
				'entrydokumen'=>$entrydokumen,
				'feedback'=>$feedback,
				'datapendukung'=>$datapendukung,
			);

		}
	}

	function update()
	{



		$config['max_size']=0;
		$config['allowed_types']="png|jpg|jpeg|gif|doc|docx|xlsx|xls|pdf";
		$config['remove_spaces']=FALSE;
		$config['overwrite']=TRUE;
		$config['upload_path']=FCPATH.'upload/files';

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

			$id = $this->input->post('id');
			$tahunpkpt = $this->input->post('tahunpkpt');
			$tanggal = $this->input->post('tanggal');
			$tanggal = date('Y-m-d', strtotime($tanggal));
			$lhp = $this->input->post('lhp');
			$temuan = $this->input->post('temuan');
			$rekomendasi = $this->input->post('rekomendasi');
			$tindaklanjut = $this->input->post('tindaklanjut');
			$entrydokumen = $this->input->post('entrydokumen');
			$feedback = $this->input->post('feedback');
			$datapendukung = $this->upload->data('file_name');


			$data = array(
				'tahunpkpt'=>$tahunpkpt,
				'tanggal'=>$tanggal,
				'lhp'=>$lhp,
				'temuan'=>$temuan,
				'rekomendasi'=>$rekomendasi,
				'tindaklanjut'=>$tindaklanjut,
				'entrydokumen'=>$entrydokumen,
				'feedback'=>$feedback,
				'datapendukung'=>$datapendukung
			);

			unlink(FCPATH.'upload/files'. $datapendukung);

			$this->model->update_data($this->table, $data, ['id' => $id]);
			redirect('daftardata');
		}
	}

	function delete()
	{
		//meminta model untuk menghapus data dengan id = $id
		$delete = $this->model->delete_data($this->table, ['id'=>$id]);

		//jika data berhasil dihapus, maka pengunjung diarahkan ke halaman show
		if($delete)
			redirect('pegawai/daftardata');

	}

}

View :
<?php $this->load->view('pegawai/header');?> 
<?php $this->load->view('pegawai/sidebar');?>
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <div class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-6">
          <h1 class="m-0 text-dark">Edit Data</h1>
        </div><!-- /.col -->
        <div class="col-sm-6">
          <ol class="breadcrumb float-sm-right">
            <li class="breadcrumb-item"><a href="<?php echo base_url('pegawai')?>">Home</a></li>
            <li class="breadcrumb-item active">Edit Data</li>
          </ol>
        </div><!-- /.col -->
      </div><!-- /.row -->
    </div><!-- /.container-fluid -->
  </div>
  <!-- /.content-header -->

  <!-- Main content -->
  <?php
  include 'koneksi.php';
  $id = $_GET['id'];
  $data = mysqli_query($koneksi,"select * from tb_data where id='$id'");
  while($d = mysqli_fetch_array($data)){
    ?>
    <section class="content">
      <div class="container-fluid">
        <form action="<?php echo base_url(). 'editdata/update'; ?>" method="post" enctype="multipart/form-data">
          <div class="row">
            <!-- left column -->
            <div class="col-md-12">
              <!-- general form elements -->
              <div class="card card-primary">
                <div class="card-header">
                </div>
                <!-- /.card-header -->
                <!-- form start -->
                <form role="form" id="quickForm">
                  <div class="card-body">
                    <div class="form-group">
                      <label for="exampleInputEmail1">Tahun PKPT</label>
                      <input type="hidden" name="id" value="<?php echo $d['id']; ?>">
                      <input type="number" class="form-control" id="exampleInputEmail1" name="tahunpkpt" value="<?php echo $d['tahunpkpt'];?>">
                    </div>
                    <div class="form-group">
                      <!-- date -->
                      <div class="form-group">
                        <div class="row">
                         <div class="col-md-4">
                          <!-- Date -->
                          <div class="form-group">
                           <label>Tanggal</label>
                           <div class="input-group date" id="tanggal" data-target-input="nearest">
                            <input type="text" name="tanggal" class="form-control datetimepicker-input" data-target="#tanggal" value="<?php echo shortdate_indo($d['tanggal'])?>">
                            <div class="input-group-append" data-target="#tanggal" data-toggle="datetimepicker">
                              <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                            </div>           
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                      <div class="col-md-12">
                        <!-- textarea -->
                        <div class="form-group">
                          <label>LHP</label>
                          <textarea  id="summernote1" name="lhp" class="form-control" rows="10"><?php echo $d['lhp'];?></textarea>
                        </div>
                      </div>   
                    </div>
                    <div class="form-group">
                      <div class="row">
                        <div class="col-md-12">
                          <!-- textarea -->
                          <div class="form-group">
                            <label>Temuan</label>
                            <textarea  id="summernote2" name="temuan" class="form-control" rows="10"><?php echo $d['temuan'];?></textarea>
                          </div>
                        </div>   
                      </div>
                      <div class="form-group">
                        <div class="row">
                          <div class="col-md-12">
                            <!-- textarea -->
                            <div class="form-group">
                              <label>Rekomendasi</label>
                              <textarea  id="summernote3" name="rekomendasi" class="form-control" rows="10"><?php echo $d['rekomendasi'];?></textarea>
                            </div>
                          </div>   
                        </div>
                        <div class="form-group">
                          <div class="row">
                            <div class="col-md-12">
                              <!-- textarea -->
                              <div class="form-group">
                                <label>Tindak Lanjut Entitas Yang Diperiksa</label>
                                <textarea  id="summernote4" name="tindaklanjut" class="form-control" rows="10"> <?php echo $d['tindaklanjut'];?></textarea>
                              </div>
                            </div>   
                          </div>
                        </div>
                        <div class="form-group">
                          <div class="row">
                            <div class="col-md-12">
                              <!-- textarea -->
                              <div class="form-group">
                                <label>Entry Dokumen Yang Diterima ITJEN</label>
                                <textarea  id="summernote5" name="entrydokumen" class="form-control" rows="10"> <?php echo $d['entrydokumen'];?></textarea>
                              </div>
                            </div>   
                          </div>
                          <div class="form-group">
                            <div class="row">
                              <div class="col-md-12">
                                <!-- textarea -->
                                <div class="form-group">
                                  <label>Feedback Satker</label>
                                  <textarea  id="summernote6" name="feedback" class="form-control" rows="10"> <?php echo $d['feedback']; ?></textarea>
                                </div>
                              </div>   
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="exampleInputFile">Data Pendukung</label>
                            <div class="input-group">
                              <div class="custom-file">
                                <div class="col-md-4">
                                  <input type="file" name="datapendukung" class="custom-file-input" id="exampleInputFile" value="">
                                  <label class="custom-file-label" for="exampleInputFile"><?php echo $d['datapendukung']; ?></label>
                                </div>
                              </div>
                            </div>
                          </div>
                          <!-- /.card-body -->
                          <br>
                          <button type="submit" class="btn btn-primary float-left">Simpan</button>
                          &nbsp;&nbsp;
                          <input type='button' value='Batal' onClick='top.location="daftardata"' class="btn btn-primary float-right">
                        </div>
                      </form>
                    </div>
                    <!-- /.card -->
                  </section>
                  <!-- /.content -->
                </div>
                <!-- /.content-wrapper -->
                <?php 
              }
              ?>
              <!-- /.content -->
            </div>
            <?php $this->load->view('pegawai/footer');?>

Model :
<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_data extends CI_Model
{
	function input_data($data,$table)
	{
		$this->db->insert($table,$data);
	}

	function update_data($table, $set, $where)
	{
		//melakukan perintah mengubah data tabel
		return $this->db->where($where)
		->update($table, $set);
	}

	function tampil_data(){
		return $this->db->get('tb_data');
	}

	public function delete_data($table, $where)
	{
		//menghapus data pada tabel $table dengan nilai kolom sama dengan $where
		return $this->db->delete($table, $where);
	}


}

Mohon Bantuannya...
avatar rachmat36

@rachmat36

2 Kontribusi 0 Poin


Jawaban

Belum ada Jawaban, jadi yang pertama

Login untuk gabung berdiskusi
premium logo tube

Hey, sedang ada diskon premium untuk akses semua kelas. Lihat di sini