par
mouhssine » 17 juin 2016, 18:00
je pose mon code , qui change les valeurs voulues , mais le problème il vide le fichier et laisse seulement les valeurs modifiées
mon fichier ini :
[GENERAL]
CODE_NATIONAL=N
CODE_REGION_INSTALLATION=011
CODE_ZONE_INSTALLATION=011
CODE_PAYS_INSTALLATION=137
CODE_GSM=200
PQMC_INSTALLATION=02
MT_IMPULSION=0.8
DUREE_MIN=1
DUREE_MAX=9999
DUREE_SONNERIE=0
COUT_UTILISE=0
NBRE_ESSAI_LOGIN=3
COEF_FACT=1
CUMUL=1
MONNAIE=DH
SOCIETE=Telepac Technology
POSTE_DEPLACE=0
IMPULTION=1
BPwd=OUI
NB_CARAC_CRYPTE=0
TYPE_APPLICATION=2
[NIVEAU]
NIV3=Service
NIV4=Devision
NIV1=Direction
NIV2=Département
NIV5=Cellule
[FONCTIONS]
FONCTION0=Directeur
FONCTION1=Directeur Adjoint
FONCTION2=Responsable
FONCTION3=Ingenieur
FONCTION4=Salarié
[SOCIETE]
BUDGET=100000008
NOM=ATLANTA
le code de la cla classe :
<?php
class ini
{
var $fichier="exemple.ini";
var $groupe="";
var $item="";
var $valeur="";
var $fichier_ini=array();
function m_fichier($arg)
{
$this->fichier=$arg;
$this->fichier_ini=null;
$this->fichier_ini=array();
// Pour utiliser parse_ini_file() par défaut, enlevez /* et */, sinon supprimez ce commentaire (conseillé)
/*if(false!==($array=@parse_ini_file($arg, TRUE)))
{
$this->fichier_ini=$array;
}
else*/if(file_exists($arg) && $fichier_lecture=file($arg))
{
foreach($fichier_lecture as $ligne)
{
$ligne_propre=trim($ligne);
if(preg_match("#^\[(.+)\]$#",$ligne_propre,$matches))
{
$groupe_curseur=$matches[1];
}
else
{
if($ligne_propre[0]!=';' && $tableau=explode("=",$ligne,2))
{
$this->fichier_ini[$groupe_curseur][$tableau[0]]=rtrim($tableau[1],"\n\r");
}
}
}
}
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
}
function m_groupe($arg)
{
$this->groupe=$arg;
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
return true;
}
function m_item($arg)
{
$this->item=$arg;
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
return true;
}
function m_put($arg, $arg_i=false, $arg_g=false, $arg_f=false)
{
if($arg_f!==false) $this->m_fichier($arg_f);
if($arg_g!==false) $this->m_groupe($arg_g);
if($arg_i!==false) $this->m_item($arg_i);
$this->fichier_ini[$this->groupe][$this->item]=$arg;
$this->valeur=$arg;
return $this->fichier." ==> [".$this->groupe."] ".$this->item."=".$this->valeur;
}
function m_count($arg_gr=false)
{
if($arg_gr===false)
return array(1=>$gr_cou=count($this->fichier_ini), 0=>$itgr_cou=count($this->fichier_ini, COUNT_RECURSIVE), 2=>$itgr_cou-$gr_cou);
else
return count($this->fichier_ini[$arg_gr]);
}
function array_groupe($arg_gr=false)
{
if($arg_gr===false)
$arg_gr=$this->groupe;
return $this->fichier_ini[$arg_gr];
}
function save()
{
$fichier_save="";
foreach($this->fichier_ini as $key => $groupe_n)
{
$fichier_save.="
[".$key."]";
foreach($groupe_n as $key => $item_n)
{
$fichier_save.="
".$key."=".$item_n;
}
}
$fichier_save=substr($fichier_save, 1);
if(file_exists($this->fichier) && reset(explode('.',phpversion()))>=5)
{
if(false===file_put_contents($this->fichier, $fichier_save))
{
die("Impossible d'écrire dans ce fichier (mais le fichier existe).");
}
}
else
{
$fichier_ouv=fopen($this->fichier,"w+");
if(false===fwrite($fichier_ouv, $fichier_save))
{
die("Impossible d'écrire dans ce fichier (Le fichier n'existe pas).");
}
fclose($fichier_ouv);
}
return true;
}
function clear()
{
$this->fichier="";
$this->groupe="";
$this->item="";
$this->valeur="";
$this->fichier_ini=null;
$this->fichier_ini=array();
}
function s_fichier()
{
$return=$this->fichier;
if(file_exists($this->fichier)) unlink($this->fichier);
$this->fichier="";
$this->valeur="";
return "fichier(".$return.") supprimé.";
}
function s_groupe()
{
$return=$this->groupe;
if(isset($this->fichier_ini[$this->groupe])) unset($this->fichier_ini[$this->groupe]);
$this->groupe="";
$this->valeur="";
return "groupe(".$return.") supprimé.";
}
function s_item()
{
$return=$this->item;
if(isset($this->fichier_ini[$this->groupe][$this->item])) unset($this->fichier_ini[$this->groupe][$this->item]);
$this->item="";
$this->valeur="";
return "item(".$return.") supprimé.";
}
function print_curseur()
{
echo "Fichier : <b>".$this->fichier."</b><br />";
echo "Groupe : <b>".$this->groupe."</b><br />";
echo "Item : <b>".$this->item."</b><br />";
echo "Valeur : <b>".$this->valeur."</b><br />";
return true;
}
function print_dossier()
{
if(is_dir($this->fichier)) {
echo "<img src='dir.png' alt='Dossier' /><span style='position:relative; top:-10px;font-size:20px; font-weight:bold;'>".$this->fichier."</span><br />";
if($handle=opendir($this->fichier))
{
while(false!==($file=readdir($handle)))
{
if(substr($file, -4, 4)==".ini")
{
echo " <a href='?fichier=".$file."'><img src='iniicone.png' alt='Ini' style='border:none;' /></a> ".$file."<br />";
}
}
closedir($handle);
}
return true; }
else { echo "L'élément sélectionné n'est pas un dossier"; return false; }
}
function print_fichier()
{
if(file_exists($this->fichier) && is_file($this->fichier) && $fichier_lecture=file($this->fichier))
{
foreach($fichier_lecture as $ligne)
{
$ligne=preg_replace("#\s$#","",$ligne);
if(preg_match("#^\[.+\]\s?$#",$ligne))
$groupe=false;
if(preg_match("#^\[".preg_quote($this->groupe, "#")."\]$#",$ligne))
{
$echo.= "<span style='background-color:aqua;'>".htmlspecialchars($ligne)."</span><br />";
$groupe=true;
}
elseif($groupe==true && $this->item==reset(explode("=",$ligne)))
$echo.= "<span style='background-color:yellow;'>".htmlspecialchars($ligne)."</span><br />";
else
$echo.= htmlspecialchars($ligne)."<br />";
}
echo $echo;
}
else
{
echo "Le fichier n'existe pas ou est incompatible";
}
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
return true;
}
function m_valeur($arg_item, $arg_groupe)
{
return $this->fichier_ini[$arg_groupe][$arg_item];
}
}
?>
mon code php suivi du html
<?php
include('C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\EagleWeb\class.php');
session_start();
if(isset($_SESSION['ini']))
$ini_objet=unserialize($_SESSION['ini']);
else
$ini_objet=new ini();
if(isset($_GET['fichier']))
{
$dossier=$ini_objet->fichier;
$dossier=preg_replace('#/$#','',$dossier).'/';
$ini_objet->m_fichier($dossier.$_GET['fichier']);
header('Location:./ini.php5');
}
if(isset($_POST['clear']))
{
$ini_objet->clear();
}
$groupe1='SOCIETE';
$groupe2='NIVEAU';
$item1='NIV1';
$item2='NOM';
$item3='BUDGET';
if(isset($_POST['valeur']))
{
if($_POST['valeur']=='Modifier')
{
$ini_objet->m_put(stripslashes($_POST['Txt_Name']),$item2,$groupe1);
$ini_objet->m_put(stripslashes($_POST['Txt_Budget']),$item3,$groupe1);
$ini_objet->m_put(stripslashes($_POST['Txt_Niveau']),$item1,$groupe2);
$ini_objet->save();
}
}
$_SESSION['ini']=serialize($ini_objet);
?>
<html>
<head>
<title>Traitement INI</title>
<style>
form
{
margin:0px;
padding:0px;
}
fieldset
{
margin-right:10px;
margin-bottom:8px;
border:1px black solid;
}
legend
{
color:#333333;
font-style:italic;
font-weight:bolder;
font-size:18px;
}
</style>
</head>
<body>
<div style="float:right;">
<div style="width:400px; padding:6px; margin-right:12px; border:1px solid black;">
<div style="float:right; text-align:right;">
<?php
$stat=$ini_objet->m_count();
echo '<strong>'.$stat[1].'éléments</strong> dans ce fichier<br />
dont <strong>'.$stat[0].'groupes</strong> et <strong>'.$stat[2].' items</strong>.<br />
<strong>'.$ini_objet->m_count($ini_objet->groupe).' items</strong> dans ce groupe.';
?>
</div>
<u><i><b>Information sur le curseur</b></i></u><br />
<?php $ini_objet->print_curseur(); ?>
</div>
<div style="width:400px; height:400px; overflow:auto; padding:6px; margin-top:12px; margin-right:12px; border:1px solid black;">
<u><i><b>Aperçu du <?php if(is_dir($ini_objet->fichier)) echo 'dossier'; else echo 'fichier'; ?></b></i></u><br /><br />
<span style="font-family:courier; white-space:nowrap;"><?php if(is_dir($ini_objet->fichier)) $ini_objet->print_dossier(); else $ini_objet->print_fichier(); ?></span>
</div>
</div>
<fieldset><legend>Valeur</legend>
<form method="post" action="">
<label>Nom : <input type="text" name="Txt_Name" /></label><br />
<label>Budget : <input type="text" name="Txt_Budget" /></label><br />
<label>Niveau : <input type="text" name="Txt_Niveau" /></label><br />
<input type="submit" name="valeur" value="Modifier" />
</form>
</fieldset>
<br />
</body>
</html>
j'attend votre aide merci
je pose mon code , qui change les valeurs voulues , mais le problème il vide le fichier et laisse seulement les valeurs modifiées
mon fichier ini :
[GENERAL]
CODE_NATIONAL=N
CODE_REGION_INSTALLATION=011
CODE_ZONE_INSTALLATION=011
CODE_PAYS_INSTALLATION=137
CODE_GSM=200
PQMC_INSTALLATION=02
MT_IMPULSION=0.8
DUREE_MIN=1
DUREE_MAX=9999
DUREE_SONNERIE=0
COUT_UTILISE=0
NBRE_ESSAI_LOGIN=3
COEF_FACT=1
CUMUL=1
MONNAIE=DH
SOCIETE=Telepac Technology
POSTE_DEPLACE=0
IMPULTION=1
BPwd=OUI
NB_CARAC_CRYPTE=0
TYPE_APPLICATION=2
[NIVEAU]
NIV3=Service
NIV4=Devision
NIV1=Direction
NIV2=Département
NIV5=Cellule
[FONCTIONS]
FONCTION0=Directeur
FONCTION1=Directeur Adjoint
FONCTION2=Responsable
FONCTION3=Ingenieur
FONCTION4=Salarié
[SOCIETE]
BUDGET=100000008
NOM=ATLANTA
le code de la cla classe :
[php]<?php
class ini
{
var $fichier="exemple.ini";
var $groupe="";
var $item="";
var $valeur="";
var $fichier_ini=array();
function m_fichier($arg)
{
$this->fichier=$arg;
$this->fichier_ini=null;
$this->fichier_ini=array();
// Pour utiliser parse_ini_file() par défaut, enlevez /* et */, sinon supprimez ce commentaire (conseillé)
/*if(false!==($array=@parse_ini_file($arg, TRUE)))
{
$this->fichier_ini=$array;
}
else*/if(file_exists($arg) && $fichier_lecture=file($arg))
{
foreach($fichier_lecture as $ligne)
{
$ligne_propre=trim($ligne);
if(preg_match("#^\[(.+)\]$#",$ligne_propre,$matches))
{
$groupe_curseur=$matches[1];
}
else
{
if($ligne_propre[0]!=';' && $tableau=explode("=",$ligne,2))
{
$this->fichier_ini[$groupe_curseur][$tableau[0]]=rtrim($tableau[1],"\n\r");
}
}
}
}
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
}
function m_groupe($arg)
{
$this->groupe=$arg;
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
return true;
}
function m_item($arg)
{
$this->item=$arg;
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
return true;
}
function m_put($arg, $arg_i=false, $arg_g=false, $arg_f=false)
{
if($arg_f!==false) $this->m_fichier($arg_f);
if($arg_g!==false) $this->m_groupe($arg_g);
if($arg_i!==false) $this->m_item($arg_i);
$this->fichier_ini[$this->groupe][$this->item]=$arg;
$this->valeur=$arg;
return $this->fichier." ==> [".$this->groupe."] ".$this->item."=".$this->valeur;
}
function m_count($arg_gr=false)
{
if($arg_gr===false)
return array(1=>$gr_cou=count($this->fichier_ini), 0=>$itgr_cou=count($this->fichier_ini, COUNT_RECURSIVE), 2=>$itgr_cou-$gr_cou);
else
return count($this->fichier_ini[$arg_gr]);
}
function array_groupe($arg_gr=false)
{
if($arg_gr===false)
$arg_gr=$this->groupe;
return $this->fichier_ini[$arg_gr];
}
function save()
{
$fichier_save="";
foreach($this->fichier_ini as $key => $groupe_n)
{
$fichier_save.="
[".$key."]";
foreach($groupe_n as $key => $item_n)
{
$fichier_save.="
".$key."=".$item_n;
}
}
$fichier_save=substr($fichier_save, 1);
if(file_exists($this->fichier) && reset(explode('.',phpversion()))>=5)
{
if(false===file_put_contents($this->fichier, $fichier_save))
{
die("Impossible d'écrire dans ce fichier (mais le fichier existe).");
}
}
else
{
$fichier_ouv=fopen($this->fichier,"w+");
if(false===fwrite($fichier_ouv, $fichier_save))
{
die("Impossible d'écrire dans ce fichier (Le fichier n'existe pas).");
}
fclose($fichier_ouv);
}
return true;
}
function clear()
{
$this->fichier="";
$this->groupe="";
$this->item="";
$this->valeur="";
$this->fichier_ini=null;
$this->fichier_ini=array();
}
function s_fichier()
{
$return=$this->fichier;
if(file_exists($this->fichier)) unlink($this->fichier);
$this->fichier="";
$this->valeur="";
return "fichier(".$return.") supprimé.";
}
function s_groupe()
{
$return=$this->groupe;
if(isset($this->fichier_ini[$this->groupe])) unset($this->fichier_ini[$this->groupe]);
$this->groupe="";
$this->valeur="";
return "groupe(".$return.") supprimé.";
}
function s_item()
{
$return=$this->item;
if(isset($this->fichier_ini[$this->groupe][$this->item])) unset($this->fichier_ini[$this->groupe][$this->item]);
$this->item="";
$this->valeur="";
return "item(".$return.") supprimé.";
}
function print_curseur()
{
echo "Fichier : <b>".$this->fichier."</b><br />";
echo "Groupe : <b>".$this->groupe."</b><br />";
echo "Item : <b>".$this->item."</b><br />";
echo "Valeur : <b>".$this->valeur."</b><br />";
return true;
}
function print_dossier()
{
if(is_dir($this->fichier)) {
echo "<img src='dir.png' alt='Dossier' /><span style='position:relative; top:-10px;font-size:20px; font-weight:bold;'>".$this->fichier."</span><br />";
if($handle=opendir($this->fichier))
{
while(false!==($file=readdir($handle)))
{
if(substr($file, -4, 4)==".ini")
{
echo " <a href='?fichier=".$file."'><img src='iniicone.png' alt='Ini' style='border:none;' /></a> ".$file."<br />";
}
}
closedir($handle);
}
return true; }
else { echo "L'élément sélectionné n'est pas un dossier"; return false; }
}
function print_fichier()
{
if(file_exists($this->fichier) && is_file($this->fichier) && $fichier_lecture=file($this->fichier))
{
foreach($fichier_lecture as $ligne)
{
$ligne=preg_replace("#\s$#","",$ligne);
if(preg_match("#^\[.+\]\s?$#",$ligne))
$groupe=false;
if(preg_match("#^\[".preg_quote($this->groupe, "#")."\]$#",$ligne))
{
$echo.= "<span style='background-color:aqua;'>".htmlspecialchars($ligne)."</span><br />";
$groupe=true;
}
elseif($groupe==true && $this->item==reset(explode("=",$ligne)))
$echo.= "<span style='background-color:yellow;'>".htmlspecialchars($ligne)."</span><br />";
else
$echo.= htmlspecialchars($ligne)."<br />";
}
echo $echo;
}
else
{
echo "Le fichier n'existe pas ou est incompatible";
}
$this->valeur=$this->fichier_ini[$this->groupe][$this->item];
return true;
}
function m_valeur($arg_item, $arg_groupe)
{
return $this->fichier_ini[$arg_groupe][$arg_item];
}
}
?>[/php]
mon code php suivi du html
[php]<?php
include('C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\EagleWeb\class.php');
session_start();
if(isset($_SESSION['ini']))
$ini_objet=unserialize($_SESSION['ini']);
else
$ini_objet=new ini();
if(isset($_GET['fichier']))
{
$dossier=$ini_objet->fichier;
$dossier=preg_replace('#/$#','',$dossier).'/';
$ini_objet->m_fichier($dossier.$_GET['fichier']);
header('Location:./ini.php5');
}
if(isset($_POST['clear']))
{
$ini_objet->clear();
}
$groupe1='SOCIETE';
$groupe2='NIVEAU';
$item1='NIV1';
$item2='NOM';
$item3='BUDGET';
if(isset($_POST['valeur']))
{
if($_POST['valeur']=='Modifier')
{
$ini_objet->m_put(stripslashes($_POST['Txt_Name']),$item2,$groupe1);
$ini_objet->m_put(stripslashes($_POST['Txt_Budget']),$item3,$groupe1);
$ini_objet->m_put(stripslashes($_POST['Txt_Niveau']),$item1,$groupe2);
$ini_objet->save();
}
}
$_SESSION['ini']=serialize($ini_objet);
?>[/php]
[html]<html>
<head>
<title>Traitement INI</title>
<style>
form
{
margin:0px;
padding:0px;
}
fieldset
{
margin-right:10px;
margin-bottom:8px;
border:1px black solid;
}
legend
{
color:#333333;
font-style:italic;
font-weight:bolder;
font-size:18px;
}
</style>
</head>
<body>
<div style="float:right;">
<div style="width:400px; padding:6px; margin-right:12px; border:1px solid black;">
<div style="float:right; text-align:right;">
<?php
$stat=$ini_objet->m_count();
echo '<strong>'.$stat[1].'éléments</strong> dans ce fichier<br />
dont <strong>'.$stat[0].'groupes</strong> et <strong>'.$stat[2].' items</strong>.<br />
<strong>'.$ini_objet->m_count($ini_objet->groupe).' items</strong> dans ce groupe.';
?>
</div>
<u><i><b>Information sur le curseur</b></i></u><br />
<?php $ini_objet->print_curseur(); ?>
</div>
<div style="width:400px; height:400px; overflow:auto; padding:6px; margin-top:12px; margin-right:12px; border:1px solid black;">
<u><i><b>Aperçu du <?php if(is_dir($ini_objet->fichier)) echo 'dossier'; else echo 'fichier'; ?></b></i></u><br /><br />
<span style="font-family:courier; white-space:nowrap;"><?php if(is_dir($ini_objet->fichier)) $ini_objet->print_dossier(); else $ini_objet->print_fichier(); ?></span>
</div>
</div>
<fieldset><legend>Valeur</legend>
<form method="post" action="">
<label>Nom : <input type="text" name="Txt_Name" /></label><br />
<label>Budget : <input type="text" name="Txt_Budget" /></label><br />
<label>Niveau : <input type="text" name="Txt_Niveau" /></label><br />
<input type="submit" name="valeur" value="Modifier" />
</form>
</fieldset>
<br />
</body>
</html>[/html]
j'attend votre aide merci