Advertisement
Guest User

import.php

a guest
Jul 29th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <?php
  2.  
  3. class Import extends CI_Controller {
  4. function __construct() {
  5. parent::__construct();
  6. $this->load->library(array('PHPExcel','PHPExcel/IOFactory','upload'));
  7. $this->load->helper(array('url','file'));
  8. }
  9.  
  10. function index(){
  11. if ($this->input->post('save')) {
  12. $fileName = $_FILES['import']['name'];
  13.  
  14. $config['upload_path'] = './assets/files/';
  15. $config['file_name'] = $fileName;
  16. $config['allowed_types'] = 'xls|xlsx|csv';
  17. $config['max_size'] = 10000;
  18.  
  19. $this->upload->initialize($config);
  20.  
  21. if(! $this->upload->do_upload('import') )
  22. $this->upload->display_errors();
  23.  
  24. $media = $this->upload->data();
  25. $inputFileName = './assets/files/'.$media['file_name'];
  26.  
  27. // Read your Excel workbook
  28. try {
  29. $inputFileType = IOFactory::identify($inputFileName);
  30. $objReader = IOFactory::createReader($inputFileType);
  31. $objPHPExcel = $objReader->load($inputFileName);
  32. } catch(Exception $e) {
  33. print_r($config);
  34. die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
  35. }
  36.  
  37. // Get worksheet dimensions
  38. $sheet = $objPHPExcel->getSheet(0);
  39. $highestRow = $sheet->getHighestRow();
  40. $highestColumn = $sheet->getHighestColumn();
  41.  
  42. // Loop through each row of the worksheet in turn
  43. for ($row = 2; $row <= $highestRow; $row++){ // Read a row of data into an array
  44. $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
  45. NULL,
  46. TRUE,
  47. FALSE);
  48. // Insert row data array into your database of choice here
  49. $data = array(
  50. "id"=> $rowData[0][1],
  51. "name"=> $rowData[0][2],
  52. "email"=> $rowData[0][3]
  53. );
  54.  
  55. $this->db->insert("member",$data);
  56. }
  57. echo "Import Success";
  58. }
  59. $this->load->view('import_view');
  60. }
  61. }
  62.  
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement