Created
August 11, 2020 03:42
-
-
Save irmanfrdev/2282a9f1d5c47a0b57856c378a647ed3 to your computer and use it in GitHub Desktop.
Fitur upload file di CodeIgniter 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function tambah() | |
{ | |
$this->load->helper('form'); | |
$this->load->library('form_validation'); | |
$this->form_validation->set_rules('userfile', 'Syarat', 'callback_validasi_file'); | |
if ($this->form_validation->run()) { | |
$upload = $this->upload->data(); | |
$file_name = $upload['file_name']; | |
$params = array( | |
'syarat' => $file_name, | |
); | |
$this->karyawan_model->add_karyawan($params); | |
$this->session->set_flashdata('success', '<div class="alert alert-success" role="alert">Data berhasil disimpan</div>'); | |
redirect('karyawan/tambah'); | |
} else { | |
$this->load->view('karyawan/tambah'); | |
} | |
} | |
public function ubah($id_karyawan = '') | |
{ | |
$data['karyawan'] = $this->karyawan_model->get_karyawan($id_karyawan)->row(); | |
if (empty($data['karyawan'])) { | |
show_404(); | |
} else { | |
$this->load->helper('form'); | |
$this->load->library('form_validation'); | |
$userfile = isset($_FILES['userfile']['name']) ? $_FILES['userfile']['name'] : ''; | |
if (!empty($userfile)) { | |
$this->form_validation->set_rules('userfile', 'Syarat', 'callback_validasi_file'); | |
} | |
if ($this->form_validation->run()) { | |
$karyawan = $data['karyawan']; | |
$file_name = $karyawan->syarat; | |
if (!empty($userfile)) { | |
if (!empty($file_name)) { | |
unlink('./public/file/' . $file_name); | |
} | |
$upload = $this->upload->data(); | |
$file_name = $upload['file_name']; | |
} | |
$params = array( | |
'syarat' => $file_name, | |
); | |
$this->karyawan_model->update_karyawan($id_karyawan, $params); | |
$this->session->set_flashdata('success', '<div class="alert alert-success" role="alert">Data berhasil disimpan</div>'); | |
redirect('karyawan/ubah/' . $id_karyawan); | |
} else { | |
$this->load->view('karyawan/ubah', $data); | |
} | |
} | |
} | |
public function hapus($id_karyawan = '') | |
{ | |
$karyawan = $this->karyawan_model->get_karyawan($id_karyawan); | |
if ($karyawan->num_rows() > 0) { | |
$karyawan = $karyawan->row(); | |
if (!empty($karyawan->syarat)) { | |
unlink('./public/file/' . $karyawan->syarat); | |
} | |
$this->karyawan_model->delete_karyawan($id_karyawan); | |
redirect('karyawan'); | |
} else { | |
show_404(); | |
} | |
} | |
public function validasi_file() | |
{ | |
$config['upload_path'] = './public/file/'; | |
$config['allowed_types'] = '*'; | |
$config['max_size'] = 2048; | |
$config['overwrite'] = FALSE; | |
$this->load->library('upload', $config); | |
if ($this->upload->do_upload()) { | |
return TRUE; | |
} else { | |
$this->form_validation->set_message('validasi_file', $this->upload->display_errors()); | |
return FALSE; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Karyawan_model extends CI_Model | |
{ | |
public function get_all_karyawan($sort = 'asc') | |
{ | |
$this->db->order_by('id_karyawan', $sort); | |
return $this->db->get('karyawan'); | |
} | |
public function get_karyawan($id_karyawan) | |
{ | |
$this->db->where('id_karyawan', $id_karyawan); | |
return $this->db->get('karyawan'); | |
} | |
public function add_karyawan($params) | |
{ | |
$this->db->insert('karyawan', $params); | |
return $this->db->insert_id(); | |
} | |
public function update_karyawan($id_karyawan, $params) | |
{ | |
$this->db->where('id_karyawan', $id_karyawan); | |
return $this->db->update('karyawan', $params); | |
} | |
public function delete_karyawan($id_karyawan) | |
{ | |
$this->db->where('id_karyawan', $id_karyawan); | |
return $this->db->delete('karyawan'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?= form_open_multipart('karyawan/tambah', 'class="form-horizontal form-material"') ?> | |
<h4 class="card-title">Tambah Peserta Seleksi Manager</h4> | |
<?= $this->session->flashdata('success') ?> | |
<div class="row"> | |
<div class="col-6"> | |
<h4>Data Peserta Seleksi Manager</h4> | |
<div class="form-group"> | |
<label class="col-md-12">Syarat</label> | |
<div class="col-md-12"> | |
<input type="file" name="userfile" class="form-control"> | |
<small>Ukuran maksimal 2MB</small> | |
<div class="text-danger"><?= form_error('userfile') ?></div> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-12"> | |
<div class="form-group"> | |
<div class="col-sm-12"> | |
<button type="submit" name="save" class="btn btn-success">Simpan</button> | |
<?= anchor('karyawan', 'Kembali', 'class="btn btn-secondary"') ?> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?= form_close(); ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?= form_open_multipart('karyawan/ubah/' . $karyawan->id_karyawan, 'class="form-horizontal form-material"') ?> | |
<h4 class="card-title">Ubah Peserta Seleksi Manager</h4> | |
<?= $this->session->flashdata('success') ?> | |
<div class="row"> | |
<div class="col-6"> | |
<h4>Data Peserta Seleksi Manager</h4> | |
<div class="form-group"> | |
<label class="col-md-12">Syarat</label> | |
<div class="col-md-12"> | |
<?php if (empty($karyawan->syarat)) : ?> | |
<p>Tidak ada file</p> | |
<?php else : ?> | |
<?= anchor('public/file/' . $karyawan->syarat, 'Download', 'target=_blank'); ?> | |
<?php endif; ?> | |
<input type="file" name="userfile" class="form-control"> | |
<small>Ukuran maksimal 2MB</small> | |
<small>*Kosongkan jika file tidak diubah</small> | |
<div class="text-danger"><?= form_error('userfile') ?></div> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-12"> | |
<div class="form-group"> | |
<div class="col-sm-12"> | |
<button type="submit" name="save" class="btn btn-success">Simpan</button> | |
<?= anchor('karyawan', 'Kembali', 'class="btn btn-secondary"') ?> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?= form_close(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment