Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Import extends CI_Controller {
- function __construct() {
- parent::__construct();
- $this->load->library(array('PHPExcel','PHPExcel/IOFactory','upload'));
- $this->load->helper(array('url','file'));
- }
- function index(){
- if ($this->input->post('save')) {
- $fileName = $_FILES['import']['name'];
- $config['upload_path'] = './assets/files/';
- $config['file_name'] = $fileName;
- $config['allowed_types'] = 'xls|xlsx|csv';
- $config['max_size'] = 10000;
- $this->upload->initialize($config);
- if(! $this->upload->do_upload('import') )
- $this->upload->display_errors();
- $media = $this->upload->data();
- $inputFileName = './assets/files/'.$media['file_name'];
- // Read your Excel workbook
- try {
- $inputFileType = IOFactory::identify($inputFileName);
- $objReader = IOFactory::createReader($inputFileType);
- $objPHPExcel = $objReader->load($inputFileName);
- } catch(Exception $e) {
- print_r($config);
- 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(
- "id"=> $rowData[0][1],
- "name"=> $rowData[0][2],
- "email"=> $rowData[0][3]
- );
- $this->db->insert("member",$data);
- }
- echo "Import Success";
- }
- $this->load->view('import_view');
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement