当前位置:网站首页>PHP reads the INI file and writes the modified content

PHP reads the INI file and writes the modified content

2022-07-05 04:22:00 Casual_ one thousand five hundred and seventy-three

Read ini File and overwrite data written to ini

updateTime.ini

# Update time record 
[fileUpdateTime]
time = ""
[sqlOtherUpdateTime]
time = "2022-06-08"
[sqlUpdateTime]
time = "2022-05-15"
// Method 1 
function write_ini_file($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);
    }
}

$timeInfo = parse_ini_file('updateTime.ini',true);
$timeInfo['sqlOtherUpdateTime']['time'] = date("Y-m-d", time());

write_ini_file($timeInfo, "updateTime.ini");
// Method 2 
function write_ini_file_tow($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;
}

$timeInfo = parse_ini_file('updateTime.ini',true);
$timeInfo['sqlOtherUpdateTime']['time'] = date("Y-m-d", time());

write_ini_file_tow($timeInfo, 'updateTime.ini', true);

原网站

版权声明
本文为[Casual_ one thousand five hundred and seventy-three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050420375887.html