buat database terlebih dahulu
create database latihan;
use latihan;
lalu buat table galerinya,
CREATE TABLE IF NOT EXISTS `galeri` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`foto` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
sekarang waktunya konfigurasi database dan autoload.
buka file database.php pada folder application/config/database.php
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'latihan';
$db['default']['dbdriver'] = 'mysql';
lalu buka file autoload.php pada folder application/config/autoload.php, cari baris kode berikut
$autoload['libraries'] = array('');
ubah menjadi
$autoload['libraries'] = array('database');
lalu cari kode
$autoload['helper'] = array('');
lalu cari kode
$autoload['helper'] = array('');
ubah menjadi
$autoload['helper'] = array('url','file','html','form');
setelah itu siapkan modelnya, berikut modelnya
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Blog_model extends CI_Model {
- function GetAll($query = ''){
- return $this->db->query($query);
- }
- return $this->db->insert($table,$data);
- }
- }
simpan file tersebut pada folder application/models, lalu buat controllernya.
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Galeri extends CI_Controller {
- function index(){
- if($this->input->post("simpan")){
- $this->load->library('upload');
- //Nama Foto dirubah
- $filename = 'gambar-'.trim(str_replace(" ","_","Foto")).'-'.date('dmYHis');
- if($this->upload_gambar($filename) == FALSE){
- echo $data['err_gambar'] = $this->upl_error;
- }else{
- $x = array(
- 'foto'=>$this->upl_name,
- $result = $this->blog_model->InsertData('galeri',$x);
- }
- }
- $data = array(
- 'galeri' =>$this->blog_model->GetAll("select * from galeri")->result_array()
- );
- $this->load->view('galeri',$data);
- }
- function upload_gambar($filename){
- $config['upload_path'] = "./gambar/";
- $config['allowed_types'] = 'jpg|jpeg|png|gif';
- $config['max_size'] = '2000';
- $config['max_width'] = '2000';
- $config['min_width'] = '2000';
- $config['file_name'] = $filename;
- $this->upload->initialize($config);
- if( ! $this->upload->do_upload('gambar')){
- $this->upl_error = $this->upload->display_errors('','');
- return FALSE;
- }else{
- $data['upload'] = $this->upload->data();
- $this->upl_name = $data['upload']['file_name'];
- return TRUE;
- }
- }
- }
function upload_gambar merupakan fungsi yang digunakan untuk memindahkan gambar pada folder kita, pada fungsi tersebut terdapat kode $config['upload_path'] = "./gambar/"; kode tersebut menandakan foto akan dipindahkan pada folder gambar, oleh karena itu buat folder gambar pada folder root sejajar dengan folder application.
setelah itu buat file viewnya
galeri.php
- <?php echo form_open_multipart('galeri') ?>
- <table>
- <tr>
- <td>Pilih Gambar</td>
- <td><input type="file" name="gambar"/></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" name="simpan" value="Simpan"/></td>
- </tr>
- </table>
- <hr/>
- <?php echo form_close();?>
- <?php foreach($galeri as $c){ ?>
- <table style="float:left" class="table">
- <tr>
- <td colspan="2"><?php echo $c['judul']?></td>
- </tr>
- <tr>
- <td><img src="<?php echo base_url()?>gambar/<?php echo $c['foto']?>" width="200"/></td>
- </td>
- </tr>
- </table>
- <?php } ?>
ya sekian ulasan mengenai upload, semoga bermanfaat, jika ada yang kurang jelas silahkan komentar saja :D
No comments:
Post a Comment