Tutorial Upload File
Tutorial Upload File
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
5. Buka folder Views dan buat sebuah file baru dengan nama “v_upload.php”
6. Test hasil sementara
7. Masih dalam file v_upload.php, lanjutkan dengan ketik script. Untuk memudahkan
silahkan copas dari file v_tambah.php dan perbaiki sehingga tampak sbb :
<div class="col-md-6">
<div class="card card-primary">
<div class="card-header">
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
9. Buka kembali file v_upload.php, tambahkan script untuk membuat table dan letakkan
dibagian bawah form, kemudian test hasil sementara
<div class="col-sm-12">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Keterangan</th>
<th>Upload Gambar</th>
</tr>
</thead>
<tbody>
<tr>
<td</td>
<td</td>
<td</td>
</tr>
</tbody>
</table>
</div>
10. Buat sebuah file model dengan nama “UploadModel.php” dan ketik script berikut :
(contoh file ProductModel.php)
<?php
namespace App\Models;
use CodeIgniter\Model;
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UploadModel;
12. Buka file v_upload.php dan tambahkan script looping table dengan foreach sbb :
<div class="col-sm-12">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Keterangan</th>
<th>Upload Gambar</th>
</tr>
</thead>
<tbody>
<?php $no=1; foreach($data as $key => $value) { ?>
<tr>
<td><?= $no++; ?></td>
<td><?= $value['ket']; ?></td>
<td>><?= $value['gambar']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
13. Buat terlebih dahulu sebuah folder dengan nama “folder_upload” didalam folder
“public” (jika ada folder public) untuk menampung data gambar
14. Buka controller Upload.php lalu dan buat function baru dengan nama “save’ sbb :
$validated = $this->validate([
'gambar' => 'uploaded[gambar]|mime_in[gambar,image/jpg,image/
gif,image/jpeg,image/png]|max_size[gambar,2000]'
]);
If ($validated == FALSE) {
Return $this->index();
}else {
$file_gambar = $this->request->getFile('gambar');
$file_gambar->move(ROOTPATH.'folder_upload');
$data = [
'ket' => $this->request->getPost('ket'),
'gambar'=>$file_gambar->getName(),
];
$this->UploadModel->insert_upload($data);
Return redirect()->to(base_url('upload/index'))->
with('success','Data Berhasil di Upload !!!');
}
}
15. Lakukan test hasil sementara
16. Buka file v_upload.php, tambahkan script dimulai dari baris ke-4 sbb :
17. Buka file v_upload dan perbaiki script <td><?= $value[‘gambar’]; ?></td> dengan
script berikut :
18. Pastikan setiap perubahan script sudah tersimpan dan lakukan test hasil akhir.
Semoga berhasil