Skip to content

Instantly share code, notes, and snippets.

@welkerc
Created August 1, 2014 23:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save welkerc/4cf6bca34deb7320687a to your computer and use it in GitHub Desktop.
Save welkerc/4cf6bca34deb7320687a to your computer and use it in GitHub Desktop.
A csv import for postgresql using php.
<?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);
?>
@maddymathan
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment