EVOLUTION-NINJA
Edit File: Import.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Import extends CI_Controller { // construct public function __construct() { parent::__construct(); // load model $this->load->model('Import_model', 'import'); $this->load->model('Model'); $this->load->helper(array('url','html','form')); } public function index() { $this->load->view('form'); } public function importFile(){ // print_r($_POST); // die(); if ($this->input->post('submit')) { $path = 'uploads/'; require_once APPPATH . "/third_party/PHPExcel.php"; $config['upload_path'] = $path; $config['allowed_types'] = 'xlsx|xls|csv'; $config['remove_spaces'] = TRUE; $this->load->library('upload', $config); $this->upload->initialize($config); if (!$this->upload->do_upload('uploadFile')) { $error = array('error' => $this->upload->display_errors()); } else { $data = array('upload_data' => $this->upload->data()); } if(empty($error)){ if (!empty($data['upload_data']['file_name'])) { $import_xls_file = $data['upload_data']['file_name']; } else { $import_xls_file = 0; } $inputFileName = $path . $import_xls_file; try { $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); $flag = true; $i=0; if(count($allDataInSheet)>0){ $this->model->table_truncate('stock_info'); foreach ($allDataInSheet as $value) { if($flag){ $flag =false; continue; } $inserdata[$i]['material'] = $value['A']; $inserdata[$i]['material_description'] = $value['B']; $inserdata[$i]['quantity_in_stock'] = $value['C']; $inserdata[$i]['open_po'] = $value['D']; $inserdata[$i]['in_transit_stock'] = $value['E']; $inserdata[$i]['month_planned_qty'] = $value['F']; $inserdata[$i]['remarks'] = $value['G']; $i++; } } $result = $this->import->insert($inserdata); // print_r($result);die(); if($result){ // echo "Imported successfully"; redirect('controller/inventory_import'); }else{ echo "ERROR !"; } } catch (Exception $e) { // print_r($inputFileName);die(); die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' .$e->getMessage()); } }else{ echo $error['error']; } } redirect('controller/outstanding_statement'); } }