index.php
------------------
<?php
include("inifiles.php");
$ini = new inifile;
$ini->Inifile("test.ini",0);
$ini->Write("test","test1","test2");
$ini->Updatefile();
?>
inifiles.php
--------------
<?
/********************************************************
* PHPMyIni By Virux *
*********************************************************
* *
* Date : 04/08/2001 *
* Version : 1.0 *
* Auteur : virgile boulanger (Virux) *
* Email : Virux@crosswinds.net *
* Site :
Http://www.passiondelphi.net *
* *
* Vous pouvez utiliser, modifier et distribuer ce script*
* a votre guise, mais cette entete doit toujours *
* accompagner ce script. *
********************************************************/
class inifile
{
/*Interface*/
/* Proprietes */
var $FFileName; // Filename of inifile
var $Sections; // Section list
/* Methodes */
/*
function Inifile($FileName); // Read file from HDD (create)
function Read($Section, $Ident, $Default) // Return(reading value)
function Write($Section, $Ident, $Value) // Return(boolean) -= false on error =-
function DeleteKey($Section, $Ident) // Return(boolean) -= false on error =-
function EraseSection($Section) // Return(boolean) -= false on error =-
function ReadSections() // Return(Array with all session name)
function ReadSectionValues($Section) // Return(Array with all value of the session name)
function SectionExists($Section) // Return(boolean)
function ValueExists($Section, $Ident) // Return(boolean)
function UpdateFile() // Write file to HDD
*/
/*Implementation*/
function Inifile($FileName)
{
$this->FFileName = $FileName;
if(@$F = fopen($this->FFileName ,"r"))
{
while (!feof($F))
{
$txt = fgets($F, 1024);
if (strlen($txt) > 2) // >2 because CRLF
{
if ($txt[0]=="[")
{
$lastsection = substr($txt,1,strpos($txt,"]")-1);
} else {
$ident = substr($txt,0,strpos($txt,"="));
$value = substr($txt,strpos($txt,"=")+1,strlen($txt));
$this->Sections[$lastsection][$ident] = $value;
}
}
}
fclose($F);
return(True);
}else{
return(False);
}
}
function Read($Section, $Ident, $Default)
{
$ReadStr = $this->Sections[$Section][$Ident];
if(strlen($ReadStr) > 0)
{
Return($ReadStr);
}else{
Return($Default);
}
}
function Write($Section, $Ident, $Value)
{
$this->Sections[$Section][$Ident] = $Value;
}
function DeleteKey($Section, $Ident)
{
$pos=0;
foreach ($this->Sections[$Section] as $ind=>$value)
{
if ($Ident == $ind){$find = True;};
if (!$find){$pos++;}
}
array_splice($this->Sections[$Section],$pos,$pos+1);
}
function EraseSection($Section)
{
$pos=0;
foreach ($this->Sections as $ind=>$value)
{
if ($Section == $ind){$find = True;};
if (!$find){$pos++;}
}
array_splice($this->Sections,$pos,$pos+1);
}
function ReadSections()
{
foreach ($this->Sections as $ind=>$value)
{
$TmpAray[] = $ind;
}
Return($TmpAray);
}
function ReadSectionValues($Section)
{
Return($this->Sections[$Section]);
}
function SectionExists($Section)
{
$find = False;
foreach ($this->Sections as $ind=>$value)
{
if ($Section == $ind){$find = True;};
}
Return($find);
}
function ValueExists($Section, $Ident)
{
$find = False;
foreach ($this->Sections[$Section] as $ind=>$value)
{
if ($Ident == $ind){$find = True;};
}
Return($find);
}
function UpdateFile()
{
if($F = fopen($this->FFileName ,"w"))
{
foreach ($this->Sections as $ind=>$value)
{
fputs($F ,"[".$ind."]n");
foreach ($this->Sections[$ind] as $ind2=>$value2)
{
fputs($F ,$ind2."=".$value2."n");
}
}
fclose($F);
}
// return $nbvisiteur;
}
}
?>