Persiapkan table dalam database, untuk gambar tipe data bisa gunakan Blob pada MySQL atau text pada Database lainnya.
CREATE TABLE IF NOT EXISTS tuser ( id tinyint(4) NOT NULL, username varchar(10) NOT NULL, password varchar(100) NOT NULL, photo blob ) ;atau jika ingin menambah field dari database yang sudah ada
ALTER TABLE tuser add column photo blob ;Setelah itu buat model dalam folder model misal dengan nama application/model/mupload.php
<?php Class Mupload extends CI_Model { function dataprofile($username) { $this -> db -> select('id, username, password,photo'); $this -> db -> from('tuser'); $this -> db -> where('username', $username); $this -> db -> limit(1); $query = $this -> db -> get(); if($query -> num_rows() == 1) { return $query->result(); } else { return false; } } } ?>Setelah model terbentuk lalu buat tampilan view nya, misal dengan nama application/views/vupload.php
<?php echo'<form enctype="multipart/form-data" action="'.base_url('index.php/cuploadpic/uploadgambar').'" method="POST"> <input type="file" name="imagefile" size="25"/> <button type="submit" name="sumit">upload</button> <br>'; if (ISSET($dataphoto)) { foreach ($dataphoto as $userdata) { echo '<img src="data:image;base64,'.$userdata->photo.'" alt="photo" height="250" width="250"> '; } ?> </form>Setelah dibuat view nya kita buat controller nya untuk menyimpang dan memanggil view tersebut. Buat file dengan nama application/controller/cuploadpic.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Cuploadpic extends CI_Controller { function __construct() { parent::__construct(); $this->load->model('mupload'); } function index() { $session_data =$this->session->userdata('logged_in'); if($this->session->userdata('logged_in')) { $session_data = $this->session->userdata('logged_in'); $data=array('username'=>$session_data['username'], 'dataphoto' => $this->mupload->dataprofile($session_data['username']) ); $this->load->view('vupload', $data); } else { //If no session, redirect to login page redirect('clogin', 'refresh'); } } function uploadgambar() { if($_FILES["imagefile"]["error"] != 0) { $session_data = $this->session->userdata('logged_in'); $data=array('username'=>$session_data['username'],'dataphoto'=>$this->mupload->dataprofile($session_data['username'] )); $this->load->view('vupload', $data); } else { $session_data = $this->session->userdata('logged_in'); $image=addslashes($_FILES['imagefile']['tmp_name']); $name=addslashes($_FILES['imagefile']['name']); $image=file_get_contents($image); $image=base64_encode($image); //echo $image; $dataupdate=array( 'photo'=>$image); $this->db->where('username', $session_data['username']); $this->db->update('tuser', $dataupdate); $data=array('username'=>$session_data['username'], 'dataphoto'=>$this->mupload->dataprofile($session_data['username']) ); $this->load->view('vupload', $data); } } } ?>
0 komentar
Berkomentarlah dengan Bahasa yang Relevan dan Sopan.. #okemasbro! ^_^