Code berikut merupakan code sederahan jika ada form upload menggunakan framewor codeigniter. Dan semoga bermanfaat.
public function add()
{
$this->data['view'] = 'desawisata/form_desawisata';
$this->data['title'] = 'Form Desa Wisata';
$this->data['form_action'] = 'office/desawisata/add';
$this->breadcrumbs->push('Add Desa Wisata', 'office/desawisata/add', 7);
if($this->input->post('submit')) {
$mfile = $_FILES['filefoto']['name'];
$config['file_name'] = rand(100,999)."_".$mfile;
$config['upload_path'] = './assets/foto_desa/';
$config['allowed_types'] = "jpg|png";
$config['max_size'] = 2048;
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//$this->upload->initialize($config);
if (!$this->upload->do_upload('filefoto')) {
$error = $this->upload->display_errors();
$this->data['pesan_error'] = 'Terjadi kesalahan : '.$error;
$this->load->view('template', $this->data);
} else {
$this->_validasi_add();
if($this->form_validation->run()) {
$id = $this->m_desawisata->kode_uuid();
$fn = $this->upload->data('file_name');
$info = array(
'idDesa' => $id,
'namaIndo' => $this->input->post('namaIndo'),
'namaEng' => $this->input->post('namaEng'),
'descIndo' => $this->input->post('descIndo'),
'descEng' => $this->input->post('descEng'),
'foto' => $fn,
);
if($this->m_desawisata->simpan($info) == TRUE) {
$this->session->set_flashdata('message_success', 'Berhasil menambah data');
echo"<script>
window.history.go(-2);
</script>";
} else {
$this->data['pesan_error'] = 'Gagal menambah data';
$this->load->view('template', $this->data);
}
} else {
$this->data['pesan_warning'] = 'Terjadi kesalahan.';
$this->load->view('template', $this->data);
}
}
} else {
$this->load->view('template', $this->data);
}
}
Loading...