Bagaimana cara upload menggunakan
codeigniter
Menggikuti tutorial CodeIgniter
User Guide Version 2.1.2 file uploading class
Langkah 1:
-Seting autoload.php (application/config)
$autoload['libraries'] =
array('database', 'session','upload');
$autoload['helper'] =
array('url','file','form');
Langkah 2:
-Buat folder uploads di folder nama
CI anda
-Masuk ke folder (application/controllers)
buat file upload.php
-Masukan kode berikut ini
<?php
class Upload extends
CI_Controller {
function __construct()
{
parent::__construct();
//$this->load->helper(array('form',
'url'));
}
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'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$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_success',
$data);
}
}
}
?>
Langkah 3:
-Masuk ke folder model
-Buat file dengan nama
model_news.php
-Masukan kode berikut ini
<?php if ( !
defined('BASEPATH')) exit('No direct script access allowed');
class ModelNews
extends CI_Model {
function
__construct()
{
parent::__construct();
}
function
getAllNews(){
$q="SELECT
* FROM tb_news";
return
$this->db->query($q);
}
function deleteById($id){
$q="DELETE FROM tb_news WHERE
id='$id'";
return
$this->db->query($q);
}
function InsertData($t,$c){
$q="INSERT
INTO `d4b6`.`tb_news` (`id`, `title`, `content`, `create`, `update`, `delete`)
VALUES (NULL, '$t', '$c', '', CURRENT_TIMESTAMP, '')";
return
$this->db->query($q);
}
}
Langkah 4:
-Masuk ke folder view/news
-Buat file dengan nama
view_input.php
-Masukan kode berikut
ini
<html>
<head>
<title>My
Form</title>
</head>
<body>
<?php echo
validation_errors(); ?>
<?php echo
form_open('news/input'); ?>
<h5>Title</h5>
<input
type="text" size="50" name="title" value=<?php
echo set_value('title');?> >
<h5>Content</h5>
<input
type="text" size="50" name="content"
value=<?php echo set_value('content');?>
>
<input
type="submit" value="Submit" />
</form>
</body>
</html>
Langkah 5:
-Masuk ke folder view
-Buat file dengan nama
upload_form.php
-Masukan 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>
</html>
Langkah 6:
-Masih di dalam folder
view
-Buat file dengan nama
upload_success.php
-Masukan kode berikut
ini
<html>
<head>
<title>Upload
Form</title>
</head>
<body>
<h3>Data Anda
Telah teruploaded!</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>
Langkah 7:
-Coba test CI anda
Hasilnya akan seperti
ini
Langkah 8:
-Coba browse dan pilih gambar, untuk di upload
TERIMAKASIH
Tidak ada komentar:
Posting Komentar