Created
August 1, 2014 23:52
-
-
Save welkerc/4cf6bca34deb7320687a to your computer and use it in GitHub Desktop.
A csv import for postgresql using php.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("include.php"); | |
ini_set("auto_detect_line_endings", true); | |
$dir = '/path_to/folder/'; | |
$files = scandir ( $dir, 1 ); | |
foreach ($files as $value) { | |
if(strlen ($value) > 2 ){ | |
$end_pos = strpos ( $value, "." ); | |
$f = substr ( $value, 0, $end_pos ); | |
$fname = $f; | |
$file = fopen($dir . $value, 'r'); | |
while (! feof($file) ) { | |
$csv = fgetcsv ( $file ); | |
$q = "INSERT INTO " . $fname . " VALUES ('" . $csv[0] . "'"; | |
for( $i = 1; $i < count ( $csv ); $i++ ) { | |
$q .= ",'" . $csv[$i] . "'"; | |
} | |
$q .= ");"; | |
pg_exec($db, $q); | |
} | |
fclose ( $file ); | |
echo "Table: $f has been imported.<br />\n"; | |
} | |
} | |
ini_set ( "auto_detect_line_endings", false ); | |
pg_close($db); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please help me on this php i'm new to php & postgres
i like to browse and import csv into postgres and export by selecting date could you please help me on this.