1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
<?php
$nom_fichier="algo.txt"; // Nom du fichier a convertir
$separateur="\t"; // Sigle de séparateur
// Ouverture connection base SQL
Require("Util.php");
$bd=new BD (NOM, PASSE, "import", SERVEUR, PORT);
// Creation tableau daffichage
// Juste pour montrer que ca marche !
Echo "<table border=1>";
If(file_exists($nom_fichier)) // Si le fichier existe, on louvre
$fp=fopen($nom_fichier, "r");
Else // Sinon erreur
{
Echo "Fichier introuvable<br>";
Exit();
}
While (!feof($fp)) // On parcours le fichier
{
$ligne=fgets($fp,4096); // On se deplace dun ligne
$liste=explode($separateur,$ligne); // Champs séparés par une tabulation
// Ici important
$col1=$liste[0];
$col2=$liste[1];
$col3=$liste[2];
$col4=$liste[3];
$col5=$liste[4];
$col6=$liste[5];
// $coln correspond aux colonnes de la table créée auparavant
If(trim($col1)!="") // Si fin fichier
{
Echo "<tr>";
}
// Important dans la base SQL
$query="insert into fichier (col1, col2, col3, col4, col5, col6)";
$query="values('$col1','$col2','$col3','$col4','$col5', $'col6')";
$resul=pg_exec($query);
If(!$resul)
{
Echo "Impossible dajouter";
}
Else
{
Echo "import
ok";
Echo "</td>";
Echo "</tr>";
}
Pg_close($bd); // Ferme SQL
Fclose($fp); // Ferme fichier txt
Echo "<table>"; // Fin du tableau
Echo "Merci, importation terminée<br>";
?> |
Partager