par
jajax » 11 août 2009, 14:09
voici à quoi ressemble le code j'ai pas tout mis mais vous pouvez déjà voir a peu près le fonctionnement :
<?
if ($btnIMPORTATION!="")
{
if ($Destination!="")
{
$explodeNom=explode(".",$Destination);
$nbExplodeNom=count($explodeNom)-1;
if (strtolower($explodeNom[$nbExplodeNom])!="csv")
{
print "Extension inconnue";
}
elseif (is_uploaded_file($_FILES['fichier']['tmp_name']))
{
if (move_uploaded_file($_FILES['fichier']['tmp_name'],$cheminDest.$dossierDest.$Destination))
{
$name_fichier=$Destination;
$Ligne=0;
$traitement=true;
$rep_open = chdir($dossierDest);
if (file_exists($name_fichier)) // Si le fichier existe, on l'ouvre
{
$fp = fopen($name_fichier, "r");
while (!feof($fp)) // On parcours le fichier
{
$ligne = fgets($fp,4096); // On se déplace d'une ligne
$liste = explode(";",str_replace('"','',$ligne)); // Champs séparés par ;
// ici important
$col1 = htmlentities(trim($liste[0]),ENT_QUOTES);
$col2 = htmlentities(strtoupper(trim($liste[1])),ENT_QUOTES);
$col3 = htmlentities(trim($liste[2]),ENT_QUOTES);
$col4 = htmlentities(trim($liste[3]),ENT_QUOTES);
$col5 = htmlentities(trim($liste[4]),ENT_QUOTES);
if($Ligne==0) // si ligne de noms des champs
{
$var=" style=\"font-weight: bold;\"";
if($col1=='A' and $col2=='B' and $col3=='C'
and $col4=='D' and $col5=='D')
{ // bon ordre/nom des champs ?
$color="#FFFFFF";
$req=$col1.",t".$col2.",t".$col3.",t".$col4.",t".$col5."";
//echo $req."<br>";
$traitement=true;
}else{
$traitement=false;
}
}else{
$var="";
}
if($Ligne!=0)
{
$mail=trim($col5);
$req="select id from table where infMAIL=\"".$mail."\"";
$res=mysql_query($req);
if (mysql_num_rows($res)>0) {
//Ici je connais la ligne qui a le doublon dans le fichier csv donc ici ke traitement ??
print "doulons ligne".$Ligne;
}
}
//le reste du code
//Insertion dans la base
//gestion des erreurs
}
}
}
}
}
}
?>
et j'ai touver ce script sur le net qui me semble utile mais je ne pas comment l'utiliser :
//fonction permettant de modifier une ligne dans un fichier
function modif_var($msg, $li, $nom)
{
$array = file($nom);
$array[$li] = "$msg";
$fo = fopen ("$nom", "r+b");
for ($l=0;$l<sizeof($array);$l++)
{
fwrite($fo, (trim($array[$l])." n"));
}
fclose ($fo);
}
voici à quoi ressemble le code j'ai pas tout mis mais vous pouvez déjà voir a peu près le fonctionnement :
[php]<?
if ($btnIMPORTATION!="")
{
if ($Destination!="")
{
$explodeNom=explode(".",$Destination);
$nbExplodeNom=count($explodeNom)-1;
if (strtolower($explodeNom[$nbExplodeNom])!="csv")
{
print "Extension inconnue";
}
elseif (is_uploaded_file($_FILES['fichier']['tmp_name']))
{
if (move_uploaded_file($_FILES['fichier']['tmp_name'],$cheminDest.$dossierDest.$Destination))
{
$name_fichier=$Destination;
$Ligne=0;
$traitement=true;
$rep_open = chdir($dossierDest);
if (file_exists($name_fichier)) // Si le fichier existe, on l'ouvre
{
$fp = fopen($name_fichier, "r");
while (!feof($fp)) // On parcours le fichier
{
$ligne = fgets($fp,4096); // On se déplace d'une ligne
$liste = explode(";",str_replace('"','',$ligne)); // Champs séparés par ;
// ici important
$col1 = htmlentities(trim($liste[0]),ENT_QUOTES);
$col2 = htmlentities(strtoupper(trim($liste[1])),ENT_QUOTES);
$col3 = htmlentities(trim($liste[2]),ENT_QUOTES);
$col4 = htmlentities(trim($liste[3]),ENT_QUOTES);
$col5 = htmlentities(trim($liste[4]),ENT_QUOTES);
if($Ligne==0) // si ligne de noms des champs
{
$var=" style=\"font-weight: bold;\"";
if($col1=='A' and $col2=='B' and $col3=='C'
and $col4=='D' and $col5=='D')
{ // bon ordre/nom des champs ?
$color="#FFFFFF";
$req=$col1.",t".$col2.",t".$col3.",t".$col4.",t".$col5."";
//echo $req."<br>";
$traitement=true;
}else{
$traitement=false;
}
}else{
$var="";
}
if($Ligne!=0)
{
$mail=trim($col5);
$req="select id from table where infMAIL=\"".$mail."\"";
$res=mysql_query($req);
if (mysql_num_rows($res)>0) {
//Ici je connais la ligne qui a le doublon dans le fichier csv donc ici ke traitement ??
print "doulons ligne".$Ligne;
}
}
//le reste du code
//Insertion dans la base
//gestion des erreurs
}
}
}
}
}
}
?>[/php]
et j'ai touver ce script sur le net qui me semble utile mais je ne pas comment l'utiliser :
[php]//fonction permettant de modifier une ligne dans un fichier
function modif_var($msg, $li, $nom)
{
$array = file($nom);
$array[$li] = "$msg";
$fo = fopen ("$nom", "r+b");
for ($l=0;$l<sizeof($array);$l++)
{
fwrite($fo, (trim($array[$l])." n"));
}
fclose ($fo);
}[/php]