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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
<html>
<header><title>Formulaire d'export d'un fichier csv dans bdd MySql</title></header>
<body>
<h1>Formulaire d'export d'un fichier CSV dans une bdd MySql</h1>
<form method="post" enctype="multipart/form-data"action="Exports.php";
<b>Merci d'indiquer l'emplacement du fichier csv à uploader: </b><br/><br/>
<input type="file" name="monfichier" file>
<input type="submit" value="Envoyer">
</form>
<?php
include ('functions.php');
// Testons si le fichier a bien été envoyé et s'il n'y a pas d'erreur
if (isset($_FILES['monfichier']) AND $_FILES['monfichier']['error'] == 0)
{
//echo 'success';
// Testons si le fichier n'est pas trop gros
if ($_FILES['monfichier']['size'] <= 10000000)
{
// Testons si l'extension est autorisée
$infosfichier = pathinfo($_FILES['monfichier']['name']);
$extension_upload = $infosfichier['extension'];
$extensions_autorisees = array('csv');
if (in_array($extension_upload, $extensions_autorisees))
{
// On peut valider le fichier et le stocker définitivement
move_uploaded_file($_FILES['monfichier']['tmp_name'], 'uploads/' .
basename($_FILES['monfichier']['name']));
echo 'L\'envoi a bien été effectué !<br/><br/>';
echo '<b>Informations sur le fichier uploadé: </b><br/>';
echo '<b>Nome du fichier : </b>' .$_FILES['monfichier']['name']. '<br/>';
echo '<b>Type du fichier : </b>' .$_FILES['monfichier']['type']. '<br/>';
echo '<b>Taille du fichier : </b>' .$_FILES['monfichier']['size']. ' Octets<br/>';
//$data = importcsv($_FILES['monfichier']['name'],true,";",1000)
//Connexion à la base de donnée
$bdd = connectMaBase();
//echo "<table>\n";
$row = 0;
$handle = fopen($_FILES['monfichier']['name'], "r");
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
if ($row == 0)
{
// this is the first line of the csv file
// it usually contains titles of columns
$num = count($data);
//echo "<thead>\n<tr>";
$row++;
$i = 0;
$champs = array($num);
for ($c=0; $c < $num; $c++)
{
//echo "<th>" . $data[$c] . "</th>";
$champs[$i] = $data[$c];
$i++;
}
for ($i = 0; $i< $num; $i++)
echo $champs[$i].' ';
//echo "</tr>\n</thead>\n\n<tbody>";
}
else
{
// this handles the rest of the lines of the csv file
$num = count($data);
//echo "<tr>";
$row++;
for ($c=0; $c < $num; $c++)
{
$bdd->exec('INSERT INTO exportcsv("'.$champs[$c].'")VALUES("'.$data[$c].'")');
//echo 'inertion en base <br/>';
//echo "<td>" . $data[$c]. "</td>";
}
//echo "</tr>\n";
}
}
fclose($handle);
//echo "</tbody>\n</table>";
}
else echo 'Le format de fichier est incorrect. Merci d\'envoyer un fichier csv';
//$today = date("y-m-d");
/*
//Connexion à la base de donnée
$bdd = connectMaBase();
$bdd->exec('INSERT INTO utilisateurs VALUES("","ms","G","25","'.$today.'")');
*/
//suppression du fichier
//unlink ('uploads/'.$_FILES['monfichier']['name']);
//suppression('uploads/' , 'exe', '60');
}
}
?>
</body>
</html> |
Partager