Hello,
I am very new to PHP and am trying to create a class that updates a flat file configuration file in the form of Key=Value
I can read the configuration file and split it up with no problem, however, I am having trouble updating the file.
The class I am working on cycles through the config file and if the given Key matches the current Key, it rewrites that line in the form of 'Key=NewValue'. If the given Key does not match the current key, it writes the line in the form of 'Key=OldValue'.
I am really stumped. I am not getting an returned errors, the class just does not work! I tried echoing out what is written to the variable so I could see what's going on, but nothing gets echoed. I can't figure it out! The code is as follows:
// THIS CODE WORKS
// Config File
$cfgitem = file("$root\include\config.dat");
// Read the file and create corresponding objects
foreach ($cfgitem as $key => $value) {
list ($name,$value) = split ('=', $value);
$config->$name = $value;
}
// END WORKING CODE
// THIS CODE DOES NOT WORK
class UpdateConfig {
var $file;
var $delimeter;
var $keyname;
var $keyval;
function Execute($file,$delimeter,$keyname,$keyval) {
$this->file = $file;
$this->delimeter = $delimeter;
$this->keyname = $keyname;
$this->keyval = $keyval;
$cfgitem = file($this->file);
foreach($cfgitem as $key => $value) {
list ($name,$value) = split($this->delimeter, $value);
if($name == $this->keyname) {
//echo "$this->keyname$this->delimeter$this->keyval\n";
$updatecfg .= "$this->keyname$this->delimeter$this->keyval\n";
} else {
//echo "$name$this->delimeter$value\n";
$updatecfg .= "$name$this->delimeter$value\n";
}
}
$newcfg = fopen($this->file, "w");
fputs($newcfg,$updatedcfg);
fclose($newcfg);
}
}
// END WORKING CODE
// Class Invocation
$updobj = new UpdateConfig;
$updobj->Execute($cfgitem,"=","Name","Aaron Bockover");
Thank you so much for your help!
-Aaron