Upload Gambar dengan CodeIgniter

Upload gambar dengan CI dilakukan dengan memanggil ’upload’ library. Dengan library ini, konfigurasi yang diperlukan hanya dilakukan untuk mendefinisikan :

1)                   ke path mana gambar akan disimpan,

2)                   tipe file apa saja yang diperbolehkan,

3)                   batasan besar file,

4)                   lebar maksimum, dan

5)                   tinggi maksimum gambar.

Setelah definisi selesai, proses upload akan dilakukan lewat fungsi do_upload:

$this->upload->do_upload('photo');

Dengan ’photo’ merupakan nama variabel dari file field. Berikut ini fungsi pada controller ’myprofile’ untuk mengubah foto pada website blog engine :

function gochange(){

$data=$this->data;

$data['title_header']=htmlentities(".: Change My Photo :.");

$this->load->view('header',$data);

$this->load->view('member/mmenu');

$config['upload_path'] = '../upload/';

$config['allowed_types'] = 'gif|jpg|png|bmp';

$config['max_size'] = '100';

$config['max_width'] = '1024';

$config['max_height'] = '768';

$this->load->library('upload',$config);

if(!$this->upload->do_upload('photo')) {

$flashdata = array('error' => true,'error_text'=>$this->upload->display_errors());

$this->session->set_flashdata($flashdata);

$this->session->set_flashdata($_POST);

redirect('myprofile/change_pic');

}else{

$data = array('upload_data'=>$this->upload->data());

$this->load->model('profile');

$this->profile->update_pic($data['upload_data']['file_name']);

redirect('myprofile/view/1');

}

$this->load->view('footer');

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s