question about replacing certain items in a file on the fly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kirilisa
Forum Commoner
Posts: 28
Joined: Fri Dec 05, 2003 4:41 pm

question about replacing certain items in a file on the fly

Post 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.
Last edited by kirilisa on Wed Sep 28, 2005 2:08 pm, edited 3 times in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Are you trying to physically update a PHP file or just change the constant values?
kirilisa
Forum Commoner
Posts: 28
Joined: Fri Dec 05, 2003 4:41 pm

Post 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 ?>)
kirilisa
Forum Commoner
Posts: 28
Joined: Fri Dec 05, 2003 4:41 pm

Post by kirilisa »

I am trying to physically update a file.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
Post Reply