Advertisement
Guest User

DB's CSV Importer

a guest
Jul 18th, 2019
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit( 24192000 );
  4. ini_set( 'memory_limit', '-1' );
  5. setlocale( LC_CTYPE, 'en_US.UTF-8' );
  6. mb_regex_encoding( 'UTF-8' );
  7.  
  8. class CSVToDB
  9. {
  10. public $queryParams = null;
  11.  
  12. function __construct()
  13. {
  14. $this->db = new PDO( 'mysql:host=localhost;dbname=peopleTemp;charset=UTF8', 'root', 'pass' );
  15. }
  16.  
  17. function insert( $colNames, $row, $tbl )
  18. {
  19. $noCols = count( $colNames );
  20. if( !$this->queryParams )
  21. {
  22. foreach( $colNames as $v )
  23. $this->queryParams .= ( $this->queryParams ) ? ', `' . $v . '` = ?' : ' `' . $v . '` = ?';
  24. }
  25.  
  26. $ins = $this->db->prepare( 'INSERT INTO ' . $tbl . ' SET ' . $this->queryParams );
  27. $i = 1;
  28. foreach( $row as $v )
  29. {
  30. if( $i > $noCols ) break;
  31. if( $i > $noCols ) continue;
  32. $ins->bindValue( $i, $v );
  33. $i++;
  34. }
  35. for( $i = $i; $i <= $noCols; $i++ )
  36. $ins->bindValue( $i, '' );
  37. $ins->execute();
  38. $EINFO = $ins->errorInfo();if( $EINFO[0] != 00000 ) { print_r( $EINFO ); }
  39. }
  40.  
  41. function handle( $tbl, $file, $seperator, $encolsure, $terminated, $createTbl = true )
  42. {
  43. echo "INITIALISING: $tbl\n";
  44. $colNames = array();
  45. $colLengths = array();
  46. $rows = array();
  47. if( !file_exists( $file ) ) die( "$file does not exist\n" );
  48. $dat = explode( $terminated, file_get_contents( $file ) );
  49. echo "LOADED CSV\n";
  50. foreach( $dat as $k => $row )
  51. {
  52. $cols = str_getcsv( $row, $seperator, $encolsure );
  53. if( $k == 0 )
  54. {
  55. foreach( $cols as $colNo => $col )
  56. {
  57. $col = preg_replace( array( '~\s{1,}~', '~[^0-9a-z_]~i' ), array( '_', '' ), mb_strtolower( $col ) );
  58. if( !$col ) $col = 'unk' . $colNo;
  59. $colNames[$colNo] = $col;
  60. }
  61. echo "SET COLUMN NAMES\n";
  62. }
  63. else
  64. {
  65. $row = array();
  66. foreach( $cols as $colNo => $col )
  67. {
  68. $col = trim( $col );
  69. if( $createTbl == true )
  70. {
  71. $len = mb_strlen( $col );
  72. if( !isset( $colLengths[$colNo] ) || isset( $colLengths[$colNo] ) && $len > $colLengths[$colNo] )
  73. $colLengths[$colNo] = $len;
  74. }
  75. $row[$colNo] = $col;
  76. }
  77. $rows[] = $row;
  78. }
  79. }
  80. echo "LOADED DATA\n";
  81. if( $createTbl == true )
  82. {
  83. $create = "CREATE TABLE $tbl ( id INT(8) UNSIGNED AUTO_INCREMENT PRIMARY KEY,";
  84. $last = count( $colNames ) - 1;
  85. foreach( $colNames as $k => $name )
  86. {
  87. $len = isset( $colLengths[$k] ) ? $colLengths[$k] : 1;
  88. $create .= "`$name` VARCHAR($len)";
  89. if( $last != $k )
  90. $create .= ',';
  91. }
  92. $create .= ')';
  93. $this->db->query( $create );
  94. $EINFO = $this->db->errorInfo();
  95. if( $EINFO[0] != 00000 )
  96. {
  97. print_r( $EINFO );
  98. die();
  99. }
  100. echo "CREATED TABLE\n";
  101. }
  102. foreach( $rows as $row )
  103. $this->insert( $colNames, $row, $tbl );
  104. echo "COMPLETE\n";
  105. }
  106. }
  107.  
  108. $p = new CSVToDB();
  109.  
  110. $files = glob( 'Path-to-files/*.csv' );
  111. $tbleName = 'China3';
  112.  
  113. foreach( $files as $k => $file )
  114. {
  115. echo "STARTING $file\n";
  116. $create = ( $k == 0 ) ? true : false;
  117. $parts = explode( '/', $file );
  118. #$tbl = preg_replace( '~\.csv~i', '', $parts[count( $parts ) - 1] );
  119. $p->handle( $tbleName , $file, ",", '"', "\r\n", $create );
  120. }
  121.  
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement