function write_ini($array,$file){
$res=array();
foreach($array as $key=>$val){
if(is_array($val)){
$res[]="[$key]";
foreach($val as $skey=>$sval){
$res[]="$skey = ".(is_numeric($sval)?$sval:'"'.$sval.'"');
}
}
else{
$res[]="$key = ".(is_numeric($val)?$val:'"'.$val.'"');
}
}
safefilerewrite($file,implode("\r\n",$res));
}
function safefilerewrite($fileName,$dataToSave){
if($fp=fopen($fileName,'w')){
$startTime=microtime(TRUE);
do{
$canWrite=flock($fp,LOCK_EX);
if(!$canWrite){
usleep(round(rand(0,100)*1000));
}
}
while((!$canWrite)and((microtime(TRUE)-$startTime)<5));
if($canWrite){
fwrite($fp,$dataToSave);
flock($fp,LOCK_UN);
}
fclose($fp);
}
}
$ini=parse_ini_file($_SERVER['DOCUMENT_ROOT']."/file.ini");
$ini['key']='value';
write_ini($ini,$_SERVER['DOCUMENT_ROOT']."/file.ini");