Code : Tout sélectionner
<html>
<head>
<title>Importer un fichier texte dans une bdd MySQL</title>
</head>
<body>
<h2>Importer un fichier texte dans une bdd MySQL</h2>
<?
switch($action)
{
/* LECTURE ET AJOUT DES DONNEES DANS LA TABLE */
case "ajouter":
/* Variables */
$bdd = "prévision"; /* Base de données */
$host= "localhost"; /* Hote (localhost en principe) */
$user= "root"; /* Utilisateur */
$pass= ""; /* Mot de passe */
/* Connexion bdd */
@mysql_connect($host,$user,$pass) or die("Impossible de se connecter à la base de données");
@mysql_select_db($bdd);
/* On cree la table */
if ($creertable)
{
$query = "CREATE TABLE $bss( rtch_attempts varchar(24) NOT NULL,rtch_successfully_seized varchar(24) NOT NULL,rtch_traffic_offered varchar(24) NOT NULL,rtch_erlang_busy_hour varchar(24) NOT NULL,sdcch_attempts varchar(24)NOT NULL, PRIMARY KEY (rtch_attempts))";
$result= MYSQL_QUERY($query);
}
/* On ouvre le fichier à importer en lecture seulement */
$fichier = $_FILES['fichier_u']['tmp_name'];
if (file_exists($fichier))
$fp = fopen("$fichier", "r");
else
{ /* le fichier n'existe pas */
echo "Fichier introuvable !<br>Importation stoppée.";
exit();
}
while (!feof($fp)) /* Et Hop on importe */
{ /* Tant qu'on n'atteint pas la fin du fichier */
$ligne = fgets($fp,2000); /* On lit une ligne */
/* On récupère les champs séparés par ; dans liste*/
$liste = explode( ";",$ligne);
/* On assigne les variables */
$rtch_attempts = $liste[0];
$rtch_successfully_seized = $liste[1];
$rtch_traffic_offered = $liste[2];
$rtch_erlang_busy_hour = $liste[3];
$sdcch_attempts = $liste[4];
/* Ajouter un nouvel enregistrement dans la table */
$query = "INSERT INTO $bss VALUES('$rtch_attempts','$rtch_successfully_seized','$rtch_traffic_offered','$rtch_erlang_busy_hour','$sdcch_attempts')";
$result= MYSQL_QUERY($query);
if(mysql_error())
{ /* Erreur dans la base de donnees, surement la table qu'il faut créer */
print "Erreur dans la base de données : ".mysql_error();
print "<br>Importation stoppée.";
exit();
}
else /* Tout va bien */
print "$rtch_attempts $rtch_successfully_seized $rtch_traffic_offered $rtch_erlang_busy_hour $sdcch_attempts <br>";
}
echo "<br>Importation terminée, avec succès.";
/* Fermeture */
fclose($fp);
MYSQL_CLOSE();
break;
/* FORMULAIRE DE CHOIX D'IMPORTATION */
default:
?>
<? echo "<form method=\"post\" action=\"$PHP_SELF\">"; ?>
Cocher les cellules à importer
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td>Table :</td>
<td> <input type="text" name="table"> </td>
</tr>
<tr>
<td>Fichier :</td>
<td> <input type="file" name="fichier"> </td>
</tr>
<tr>
<tr>
<td>rtch_attempts :</td>
<td> <input type="checkbox" name="creertable" checked> </td>
</tr>
<tr>
<tr>
<td>rtch_successfully_seized:</td>
<td> <input type="checkbox" name="creertable" checked> </td>
</tr>
<tr>
<tr>
<td>rtch_traffic_offered</td>
<td> <input type="checkbox" name="creertable" checked> </td>
</tr>
<tr>
<tr>
<td>rtch_erlang_busy_hour</td>
<td> <input type="checkbox" name="creertable" checked> </td>
</tr>
<tr>
<tr>
<td>sdcch_attempts</td>
<td> <input type="checkbox" name="creertable" checked> </td>
</tr>
<tr>
<td></td>
<td> <input type="submit" name="submit" value="Importer !"> </td>
</tr>
</table>
<input type="hidden" name="action" value="ajouter">
</form>
<?
break;
}
?>
</body>
</html>