Page 1 of 1

question about replacing certain items in a file on the fly

Posted: Wed Sep 28, 2005 1:25 pm
by kirilisa
I have a config file. It looks like this.

Code: Select all

config.php
<?
define('BOX_X', 22);
define('BOX_Y', 12);
define('BOX_WIDTH', 114);
define('BOX_HEIGHT', 27);
define('ENVELOPE_WIDTH', 240);	
define('DESIRED_MARGIN', 13);
?>


I have a form where a user can give new integer values for the items defined above.
e.g. after posting the form, I have something like:

Code: Select all

Array
(
    [BOX_X] => 35
    [BOX_Y] => 15
    [BOX_WIDTH] => 127
    [BOX_HEIGHT] => 30
    [ENVELOPE_WIDTH] => 200
    [DESIRED_MARGIN] => 10
    [submit] => Submit Changes
)
I am wondering, how would I read through config.php line by line, and on each line, change the value of the defined thing to the new value as specified by the user's form?

I'm figuring it's going to be some king of ereg_replace type thing but I'm really horrible at regex, and also I wasnt' sure what the best way is to read through a file line by line, if I should be using fgets() or just file().

Thanks.

Posted: Wed Sep 28, 2005 1:28 pm
by shiznatix
fgets would be the best way to read line by line probably. you should just use file_get_contents then use a series of preg_replace to write in the new values. just preg_repalce define('BOX_Y', 12); with define('BOX_Y', $newvalue); or whatnot. then you can just write over the old file with the new information

Posted: Wed Sep 28, 2005 1:30 pm
by hawleyjr
Are you trying to physically update a PHP file or just change the constant values?

Posted: Wed Sep 28, 2005 1:37 pm
by kirilisa
I have tried

$file = file_get_contents('../../common/INVOICE_CONFIG.php') or die("can't get file contents");

but echoing $file gives me nothing at all.

It won't read into the string anything in the file that between the <? and ?> (in this case, everything in the config file is between <? and ?>)

Posted: Wed Sep 28, 2005 1:37 pm
by kirilisa
I am trying to physically update a file.

Posted: Wed Sep 28, 2005 3:07 pm
by Chris Corbyn
Untested.....

Code: Select all

//Read values from the config file
$config_file_path = '/some/file.php';
$config_lines = file($config_file_path); //Get array of lines
$config_values = array(); //Get ready to stack our config values
foreach ($config_lines as $v)
{
    if (preg_match('/^define\(\'(\w+)\',\s*(.*?)\);/i', trim($v), $match))
    {
        $config_values[$match[1]] = $match[2]; //CONFIG => VALUE
    }
}

//We now have an array of current values.....

//Let's update something.
//Assume that you had a config for "FOO" and it's new value arrived in $_POST['new_config']['FOO']
foreach ($_POST['new_config'] as $k => $v)
{
    $config_values[$k] = $v; //Update (or create new) value
}

//Prepare the data ready for writing back to the file...
$new_config_data = '<?php'."\n"; //Or \r\n for windows servers
foreach ($config_values as $k => $v)
{
    $new_config_data .= 'define("'.$k.'", '.$v.');'."\n"; //Add the new line
}
$new_config_data .= '?>'."\n";

//Open config file for writing...
$handle = fopen($config_file_path, 'w');
if (fwrite($handle, $new_config_data)) echo 'Success!'; //Write to the file
else echo 'Something went wrong';
fclose($handle); //Close the file
EDIT | Added the closing <?php ?> tag