PHP to update a flat file.

[adrotate group=”3,4,7″]
As part of the ongoing update to our RAP product we need to provide a configuration file that could store information about the systems the product is running on. This file will have to be updated if a roleswap occurred which got us wondering how to update a flat file with PHP.

The problem is the flat file has to be rewritten as soon as you change any of the data within it, you cannot just update a section in the middle of the file. So we needed to develop a solution that would read the existing file, find the particular line we were interested in and then update just that line.

here is a sample of the file we would be reading

#config file ha4i.conf
# this is the *MGT or Source server, on a switch this
# variable has to be changed to reflect the current server
# type.
src_server=shield2
# this is the target server, the same applies as above
tgt_server=shield3
# this is the installation library for HA4i, it
# should be set on installation.
install_lib=HA4I61
# the time out variable for persistent connections
timeout=”300″
# the number of seconds before a page refresh occurs for
# the main monitoring page.
refresh=”250″
# the maximum number of messages to display from a message queue
maxmsg=200
# the number of characters before text wrapping occurs when
# displaying the email messages
wrap=60
# the minimum messages level to be retrieved
maxsev=0
# Valid User Name, if this is unset you must also unset the usr
# and pwd entry to allow the connections to access the IBM i systems.
# the user name and password must be correct for both the source and
# target systems.
# valid_usr can be set to any character string
# usr must be set to a IBM i profile which exists on both systems
# and has enough authority to use HA4i
# pwd is the password for the profile and should be the same on both systems
# valid_usr=User_Name
# usr=Profile_Name
# pwd=Password

As you can see the file is a number of lines with some that are comments (begin with ‘#’) and others carry the elements we are interested in. So we needed to split out the lines so only the elements with a xxx=xxx format would be read, we could then identify the actual element we were interested in before writing those elements back to the file.

Here is the code we wrote to handle this..


function switch_sys() {
$config_file = "scripts/ha4i.conf";
$comment = "#";
// create the array
$lines = file($config_file);
// copy the array elements for src & tgt
foreach($lines as $val) {
if(!strchr($val,"#")) {
$pieces = explode("=",$val);
if($pieces[0] == 'src_server')
$new_tgt = $pieces[1];
elseif($pieces[0] == 'tgt_server')
$new_src = $pieces[1];
}
}
// update the lines array
$count = sizeof($lines);
for($i = 0; $i < $count; $i++) {
if(!strchr($lines[$i],"#")) {
$pieces = explode("=",$lines[$i]);
if($pieces[0] == 'src_server') {
$lines[$i] = 'src_server=' .$new_src;
}
elseif($pieces[0] == 'tgt_server') {
$lines[$i] = 'tgt_server' .$new_tgt;
}
}
}
// now write the lines back to the file
// open the config file
$fp = fopen($config_file, "w");
if(!$fp) {
echo("Failed to open file");
return 0;
}
for($i = 0; $i < $count; $i++) {
fputs($fp,$lines[$i]);
}
fclose($fp);
}

A couple of points about the above code. Firstly we have used the file() function to create an array of the file contents. Next we have to loop through that array to find the two elements we are interested in, the src_server and tgt_server elements (we need to reverse these when a switch occurs). Once we have found each element we store its content in a couple of variables after exploding the line into a couple of elements divided by the “=” sign. Now we have the new variable we need to write them back to the array, I could not write them back immediately as I could not be certain they would be in the same order. So we loop through the array again looking for the same elements and updating the $line element as required. Now that the array has the updated strings we can write the data back to the file!

This is the result

#config file ha4i.conf
# this is the *MGT or Source server, on a switch this
# variable has to be changed to reflect the current server
# type.
src_server=shield3
# this is the target server, the same applies as above
tgt_server=shield2
# this is the installation library for HA4i, it
# should be set on installation.
install_lib=HA4I61
# the time out variable for persistent connections
timeout=”300″
# the number of seconds before a page refresh occurs for
# the main monitoring page.
refresh=”250″
# the maximum number of messages to display from a message queue
maxmsg=200
# the number of characters before text wrapping occurs when
# displaying the email messages
wrap=60
# the minimum messages level to be retrieved
maxsev=0
# Valid User Name, if this is unset you must also unset the usr
# and pwd entry to allow the connections to access the IBM i systems.
# the user name and password must be correct for both the source and
# target systems.
# valid_usr can be set to any character string
# usr must be set to a IBM i profile which exists on both systems
# and has enough authority to use HA4i
# pwd is the password for the profile and should be the same on both systems
# valid_usr=User_Name
# usr=Profile_Name
# pwd=Password

I am sure we could do this in another way, but several tries at finding a shorter way to do it ended in failure! So if like use you want to read a flat file looking for a specific entry and then update that entry this is one way you can do it!

Happy PHP’ing…

Chris…