1

i am using the function write_php_ini to write files

see the accepted answer with around 40 upvotes at:

create ini file, write values in PHP

originally i was copying from here:

How to read and write to an ini file with PHP

from the answer that is not accepted, because they looked so similar

the problem is, what if 2 process write to it at the same time? (that is, the second post seems to handle it, whereas the first one does not)

how to avoid this problem?

this is the code:

 public static function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {
$content = "";
if ($has_sections) {
  foreach ($assoc_arr as $key=>$elem) {
    $content .= "[".$key."]\n";
    foreach ($elem as $key2=>$elem2) {
      if(is_array($elem2))
      {
        for($i=0;$i<count($elem2);$i++)
        {
          $content .= $key2."[] = \"".$elem2[$i]."\"\n";
        }
      }
      else if($elem2=="") $content .= $key2." = \n";
      else $content .= $key2." = \"".$elem2."\"\n";
    }
  }
}
else {
  foreach ($assoc_arr as $key=>$elem) {
    if(is_array($elem))
    {
      for($i=0;$i<count($elem);$i++)
      {
        $content .= $key."[] = \"".$elem[$i]."\"\n";
      }
    }
    else if($elem=="") $content .= $key." = \n";
    else $content .= $key." = \"".$elem."\"\n";
  }
}

if (!$handle = fopen($path, 'w')) {
  return false;
}

$success = fwrite($handle, $content);
fclose($handle);

return $success;

}

Community
  • 1
  • 1
Toskan
  • 13,911
  • 14
  • 95
  • 185
  • Is the code above not working? Because you seem to be doing the right thing with regard to locking with `flock()`. – Will May 26 '16 at 01:24
  • 1
    that is so funny... i actually searched for the stackoverflow post and found it, copying the code from there instead of from my source code. And it was actually a very similar stackoverflow post, but not the same, check out this one. – Toskan May 26 '16 at 01:29

0 Answers0