Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit( 24192000 );
- ini_set( 'memory_limit', '-1' );
- setlocale( LC_CTYPE, 'en_US.UTF-8' );
- mb_regex_encoding( 'UTF-8' );
- class CSVToDB
- {
- public $queryParams = null;
- function __construct()
- {
- $this->db = new PDO( 'mysql:host=localhost;dbname=peopleTemp;charset=UTF8', 'root', 'pass' );
- }
- function insert( $colNames, $row, $tbl )
- {
- $noCols = count( $colNames );
- if( !$this->queryParams )
- {
- foreach( $colNames as $v )
- $this->queryParams .= ( $this->queryParams ) ? ', `' . $v . '` = ?' : ' `' . $v . '` = ?';
- }
- $ins = $this->db->prepare( 'INSERT INTO ' . $tbl . ' SET ' . $this->queryParams );
- $i = 1;
- foreach( $row as $v )
- {
- if( $i > $noCols ) break;
- if( $i > $noCols ) continue;
- $ins->bindValue( $i, $v );
- $i++;
- }
- for( $i = $i; $i <= $noCols; $i++ )
- $ins->bindValue( $i, '' );
- $ins->execute();
- $EINFO = $ins->errorInfo();if( $EINFO[0] != 00000 ) { print_r( $EINFO ); }
- }
- function handle( $tbl, $file, $seperator, $encolsure, $terminated, $createTbl = true )
- {
- echo "INITIALISING: $tbl\n";
- $colNames = array();
- $colLengths = array();
- $rows = array();
- if( !file_exists( $file ) ) die( "$file does not exist\n" );
- $dat = explode( $terminated, file_get_contents( $file ) );
- echo "LOADED CSV\n";
- foreach( $dat as $k => $row )
- {
- $cols = str_getcsv( $row, $seperator, $encolsure );
- if( $k == 0 )
- {
- foreach( $cols as $colNo => $col )
- {
- $col = preg_replace( array( '~\s{1,}~', '~[^0-9a-z_]~i' ), array( '_', '' ), mb_strtolower( $col ) );
- if( !$col ) $col = 'unk' . $colNo;
- $colNames[$colNo] = $col;
- }
- echo "SET COLUMN NAMES\n";
- }
- else
- {
- $row = array();
- foreach( $cols as $colNo => $col )
- {
- $col = trim( $col );
- if( $createTbl == true )
- {
- $len = mb_strlen( $col );
- if( !isset( $colLengths[$colNo] ) || isset( $colLengths[$colNo] ) && $len > $colLengths[$colNo] )
- $colLengths[$colNo] = $len;
- }
- $row[$colNo] = $col;
- }
- $rows[] = $row;
- }
- }
- echo "LOADED DATA\n";
- if( $createTbl == true )
- {
- $create = "CREATE TABLE $tbl ( id INT(8) UNSIGNED AUTO_INCREMENT PRIMARY KEY,";
- $last = count( $colNames ) - 1;
- foreach( $colNames as $k => $name )
- {
- $len = isset( $colLengths[$k] ) ? $colLengths[$k] : 1;
- $create .= "`$name` VARCHAR($len)";
- if( $last != $k )
- $create .= ',';
- }
- $create .= ')';
- $this->db->query( $create );
- $EINFO = $this->db->errorInfo();
- if( $EINFO[0] != 00000 )
- {
- print_r( $EINFO );
- die();
- }
- echo "CREATED TABLE\n";
- }
- foreach( $rows as $row )
- $this->insert( $colNames, $row, $tbl );
- echo "COMPLETE\n";
- }
- }
- $p = new CSVToDB();
- $files = glob( 'Path-to-files/*.csv' );
- $tbleName = 'China3';
- foreach( $files as $k => $file )
- {
- echo "STARTING $file\n";
- $create = ( $k == 0 ) ? true : false;
- $parts = explode( '/', $file );
- #$tbl = preg_replace( '~\.csv~i', '', $parts[count( $parts ) - 1] );
- $p->handle( $tbleName , $file, ",", '"', "\r\n", $create );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement