用php讀寫ini檔

http://php.net
parse-ini-file有提到類似資料

檔案名稱:listconfig.ini
讀取現有的數據 做成表格form然後發送給ini_update.php上傳
*也可改成ajax方式傳給ini_update.php

index.php

<html>
<body>
<?php 
 
//parse the ini file using default parse_ini_file() PHP function
$parsed_ini = parse_ini_file("listconfig.ini", true);
 
//after the form submit
 

?>
 
<form action="ini_update.php" method="post">
	<?php 
		
	foreach($parsed_ini as $section=>$values){
		echo "<h3>$section</h3>";
		//keep the section as hidden text so we can update once the form submitted
		echo "<input type='hidden' value='$section' name='$section' />";
		//print all other values as input fields, so can edit. 
		//note the name='' attribute it has both section and key
		foreach($values as $key=>$value){
			echo "<p>".$key.": <input type='text' name='{$section}[$key]' value='$value' />"."</p>";
		}
		echo "<br>";
	}
 
	?>
	<input type="submit" value="Update INI" />
</form>
</body>
</html>

ini_update.php

<?php 
 if($_POST){
	$data = $_POST;
	//update ini file, call function
	echo update_ini_file($data, "listconfig.ini");
}
 
//this is the function going to update your ini file
	function update_ini_file($data, $filepath) { 
		$content = ""; 
		
		//parse the ini file to get the sections
		//parse the ini file using default parse_ini_file() PHP function
		//$parsed_ini = parse_ini_file("listconfig1.ini", true);
		
		foreach($data as $section=>$values){
			//append the section 
			$content .= "[".$section."]\n"; 
			//append the values
			foreach($values as $key=>$value){
				$content .= $key."=".$value."\n"; 
			}
		}
		
		//write it into file
		if (!$handle = fopen("listconfig.ini", 'w')) { 
			return false; 
		}
 
		$success = fwrite($handle, $content);
		fclose($handle); 
 
		return "寫入數據:" . $success; 
	}
?>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值