Anda di halaman 1dari 3

Ahmad Subki 2012

Okey sekarang kita kembali lagi, kali ini saya akan share ilmu, bagaimana cara upload File menggunakan CodeIgniter. Semoga bermanfaat Ikuti langkah langkah ini ya 1. Pertama Setting Autoload.php ( masak lupa tempat folder filenya? ) $autoload['libraries'] = array('database','session','upload'); $autoload['helper'] = array('url', 'file','form');

2. Selanjutnya Setting juga Buat Folder Uploads di folder CI anda 3. Masuk yuk ke controller kemudian buat file baru dengan nama upload.php dan ketik kode berikut ini <?php class Upload extends CI_Controller { function __construct() { parent::__construct(); } function index() { $this->load->view('upload_form', array('error' => ' ' )); } function do_upload() { $config['upload_path'] = 'uploads'; $config['allowed_types'] = 'gif|jpg|png'; //$config['max_size'] = '250'; $this->load->library('upload', $config); $this->upload->initialize($config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_succes', $data); } } } ?>

Ahmad Subki 2012


4. Sekarang kita loncat ke folder model dengan nama model_news.php dan isi file tersebut dengan kode berikut ini : <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Model_news extends CI_Model { public function __construct() { parent ::__construct(); } function getAllNews(){ $q="SELECT * FROM news"; return $this->db->query($q); } function getinputnews($title,$content){ $q="insert into news set title='$title',content='$content'"; return $this->db->query($q); } } 5. Selanjutnya lebih lanjutnya buat file newsinput.php didalam folder view/news :

<html> <head> <title>My Form</title> </head> <body> <?php echo validation_errors(); ?> <?php echo form_open('news/input'); ?> <h5>title</h5> <input type="text" name="title" value="" size="50" /> <h5>content</h5> <input type="text" name="content" value="" size="50" /> <div><input type="submit" value="Submit" /></div> </form> </body> </html>
6. Sekarang buat file didalam folder view dengan nama upload_form.ph dan upload_succes.php dan ketikkan kode berikut ini : <html> <head> <title>Upload Form</title> </head> <body> <?php echo $error;?> <?php echo form_open_multipart('upload/do_upload');?> <input type="file" name="userfile" size="20" /> <br /><br /> <input type="submit" value="upload" /> </form> </body>

Ahmad Subki 2012

<html> <head> <title>Upload Form</title> </head> <body> <h3>Your file was successfully uploaded!</h3> <ul> <?php foreach ($upload_data as $item => $value):?> <li><?php echo $item;?>: <?php echo $value;?></li> <?php endforeach; ?> </ul> <p><?php echo anchor('upload', 'Upload Another File!'); ?></p> </body> </html> 7. Sekarang Jalankan File anda semoga hasilnya seperti ini

TERIMAKASIH