Skip to content

Instantly share code, notes, and snippets.

@mdestafadilah
Created August 15, 2019 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdestafadilah/e261f5c4cd33f89a4c705f7a56c45650 to your computer and use it in GitHub Desktop.
Save mdestafadilah/e261f5c4cd33f89a4c705f7a56c45650 to your computer and use it in GitHub Desktop.
Fungsi Upload
function disposisi_upload()
{
$this->load->library(array('PHPExcel','PHPExcel/IOFactory'));
if ($this->input->post('save')) {
$type = explode('.', $_FILES["import"]["name"]); // data file
$type = strtolower($type[count($type)-1]); // data type like .jpg
// exit(dump($type));
$inputFileName = "./assets/upload/".uniqid(rand()).'.'.$type; // hash unik
if(in_array($type, array("xls", "xlsx"))) {
if(is_uploaded_file($_FILES["import"]["tmp_name"])) {
if(move_uploaded_file($_FILES["import"]["tmp_name"],$inputFileName)) {
// Read your Excel workbook
try {
$inputFileType = IOFactory::identify($inputFileName);
$objReader = IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 2; $row <= $highestRow; $row++) { // Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
// Insert row data array into your database of choice here
$data = array(
"nama" => $rowData[1][3],
"kode_org" => $rowData[1][2],
);
// exit(dump($rowData[0][1]));
$this->db->insert("sdm_master_jabatan_detail",$data); // exit(show_last_query());
}
echo "Import Success, tunggu sebentar..";
}
}
}
}
$this->setFlash('Data eksel berhasil diimport!', 'alert-success');
redirect('office/upload_disposisi','refresh');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment