Skip to main content

Membuat Aplikasi Upload Image dengan Codeigniter.

September 28, 2011 by fanjava

fanjava's picture

Salam para penggemar Codeigniter.

Mungkin tutorial ini sudah banyak dilihat diluar sana :) Tapi saya ingin sekedar share sekalian sebagai catatan buat saya sendiri. Semoga membantu.

Ini tutorial Library Upload. Yg digunakan untuk upload..wkwk

Buat file upload_view di ./application/views/

<html>
	<head>
		<title>Upload Codeigniter</title>
	</head>
 
	<body>
		<?php echo $error; ?>
		<?php echo form_open_multipart('upload/do_upload'); ?>
		<input type="file" name="user_file" size="20" />
		<br /><br />
		<input type="submit" value="Upload" />
		<?php echo form_close(); ?>
	</body>
</html>

Buat file upload_success di ./application/views/

<?php
	echo "Upload Success!";
?>

Kemudian untuk controller upload.php di ./application/controllers/

<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Upload extends CI_Controller{
	function __construct()
	{
		parent::__construct();
 
		//load helper Form dan URL
		$this->load->helper(array('form','url'));
	}
 
	function index()
	{
		$this->load->view('upload_view', array('error' => ''));
	}
 
	function do_upload()
	{
 
		//Konfigurasi untuk upload filenya
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '100';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
 
		//load library Upload dengan Konfigurasi yang sudah dibuat.
		$this->load->library('upload', $config);	
 
		//Jika gagal upload..
		if( ! $this->upload->do_upload('user_file')) /* user_file adalah name dari field di bagian View */
		{
			//cetak error
			$error = array('error' => $this->upload->display_errors());
			$this->load->view('upload_view', $error);
		}else // jika berhasil
		{
			//upload data
			$data = array('upload_data' => $this->upload->data());
 
			//load view SUCCESS
			$this->load->view('upload_success', $data);
		}
	}
}

Jangan lupa membuat folder 'uploads' di ROOT codeigniter kamu!

Untuk keterangannya udah ada di Masing2 file..Hehehe *males ngetik :)

Smeoga membantu. Ada yang ditanyakan? Jangan lupa komentarnya :)
Ayoo buat web forum ini maju!!

Premium Drupal Themes by Adaptivethemes