创建ini文件,用PHP写值

我找不到一种方法可以轻松地让我创建一个新文件,将其视为ini文件(而不是php.ini或类似文件……针对每个用户的单独ini文件),以及使用PHP创建/删除值。PHP似乎没有提供创建ini文件和读取/写入/删除值的简便方法。到目前为止,所有内容都只是“读”的,与创建条目或操作键/值无关。



慕丝7291255
浏览 843回答 3
3回答

绝地无双

我无法保证其效果如何,但是在的文档页面上有一些建议实现相反的建议parse_ini_file()(即write_ini_file,这不是标准的PHP函数)parse_ini_file。您可以使用write_ini_file将值发送到文件,parse_ini_file读回它们-修改parse_ini_file返回的关联数组,然后使用将该修改后的数组写回到文件中write_ini_file。那对你有用吗?

HUX布斯

从PHP文档的注释中找到了以下代码片段:function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {&nbsp;&nbsp; &nbsp; $content = "";&nbsp;&nbsp; &nbsp; if ($has_sections) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($assoc_arr as $key=>$elem) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $content .= "[".$key."]\n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($elem as $key2=>$elem2) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(is_array($elem2))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for($i=0;$i<count($elem2);$i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $content .= $key2."[] = \"".$elem2[$i]."\"\n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if($elem2=="") $content .= $key2." = \n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else $content .= $key2." = \"".$elem2."\"\n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; else {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($assoc_arr as $key=>$elem) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(is_array($elem))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for($i=0;$i<count($elem);$i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $content .= $key."[] = \"".$elem[$i]."\"\n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if($elem=="") $content .= $key." = \n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else $content .= $key." = \"".$elem."\"\n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; if (!$handle = fopen($path, 'w')) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; $success = fwrite($handle, $content);&nbsp; &nbsp; fclose($handle);&nbsp;&nbsp; &nbsp; return $success;&nbsp;}用法:$sampleData = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first-1' => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first-2' => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first-3' => 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first-4' => 4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first-5' => 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second-1' => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second-2' => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second-3' => 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second-4' => 4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second-5' => 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));write_ini_file($sampleData, './data.ini', true);祝好运!

交互式爱情

PEAR有两个(经过单元测试)软件包,可以完成您需要的任务:Config_Lite-如果只需要.ini文件,则非常理想配置 -也是读取.php和.xml文件我宁愿使用经过良好测试的代码,也不愿编写自己的代码。
打开App,查看更多内容
随时随地看视频慕课网APP