Show DevBest [PHP] [CLASS] INIFile: Manipulating ini files

Status
Not open for further replies.

X1M!

le troller
Jan 6, 2011
179
1
I used this for my latest project, wich required a ini-file editing class, and I felt like releasing it, so.. Enjoy!

PHP:
<?php

#########################################
#            INI-FILE CLASS             #
#---------------------------------------#
#     WRITTEN FROM SCRATCH BY X1M!      #
#########################################
# PLEASE DO NOTE: THIS DOES NOT SUPPORT #
#        SECTIONS INSIDE THE FILE.      #
#########################################
#        USED FOR: INI DATABASES        #
#########################################

class INIFile
{
	function editFile($path, $newContents)
	{
		$fh = fopen($path, "w") or die("CANT OPEN FILE: ".$path.".");
		fwrite($fh, $newContents);
		fclose($fh);
	}
	
		function appendFile($path, $newContents)
	{
		$fh = fopen($path, "a") or die("CANT OPEN FILE: ".$path.".");
		fwrite($fh, $newContents);
		fclose($fh);
	}
	
	function getVariableValue($path, $variable)
	{
		$ret = Parse_Ini_File($path) or die("FILE: ".$path." DOES NOT EXIST OR HAS NO STRINGS.");
		if(array_key_exists($variable, $ret)) {
			return $ret[$variable];
		}else{
			return 0;
		}
	}
	
	function setVariableValue($path, $variable, $value)
	{
		chmod($path, 0777);
		$ret = Parse_Ini_File($path) or die("FILE: ".$path." DOES NOT EXIST");
		$this->editFile($path, ""); // Delete previous content.
		foreach($ret as $key => $keyvalue)
		{
			if($key != $variable) {
				$this->appendFile($path, $key." = ".$keyvalue."\n");
			}else{
				$this->appendFile($path, $variable." = ".$value."\n");
			}
		}
		chmod($path, 0755);
		return 1;
	}
}

// Usage: $INIFile = new INIFile();

?>
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
That's very nice!
I would like to see more releases like this from you ;P
Also, what was the project about, for an example on what you would use this.
 

X1M!

le troller
Jan 6, 2011
179
1
I built a small flatfile CMS to use for a portfolio-like site, it failed but I released the script. :p

The portfolio was designed to have a small guestbook and a list with projects, information and download links.
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,194
3,901
Looks good, I see your a good PHP coder, You should of joined earlier and entered the coding competition, You'd of done good. - Anyways, Nice release, Like to see more from you.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
This is a really good code. Easy from my perspective but very useful towards other users.
You should try commenting the code so PHP noobies know what the lines of code mean.
 

X1M!

le troller
Jan 6, 2011
179
1
Yeah, I am not much of commenting code's atm, I have just started with OOP.
 
Status
Not open for further replies.

Users who are viewing this thread

Top