Page 1 of 1
Edit file contents
Posted: Mon Feb 13, 2006 1:10 pm
by Brian3864
Surprisingly I can't find the answer to this anywhere. I want to be able to modify file contents using a form. Let's say I have a configuration file with
Code: Select all
$server = 'localhost';
$username = 'username';
$password = 'password';
Along with other code. I would like to place each of the values into an array or seperate variables to load into a form for editing. I can probably figure out how to load them into the variable, but can't find how to append the data already in the file.
Posted: Mon Feb 13, 2006 1:23 pm
by josh
Well you can just include the config file, then modify only the ones modified in the form, then write back in the contents, to write back in the contents I recommend an array
Code: Select all
$config='';
foreach($array as $name => $value) {
$config.= '$'.$name.'="'.$value.'";\n";
}
echo $config;
Edit file contents
Posted: Mon Feb 13, 2006 1:37 pm
by Brian3864
I want the form to update the file using their settings so that all of the pages with the config file included will have the correct settings. I assume I would need to place each line of the config file into an array, loop through the array until it found the $server, $username, $password at the beginning of the array, change the value for each of those and then rewrite the config file with the new settings. Questions
1. Is that the easiest/best/only way to do this?
2. Is there a way to just rewrite a couple of lines in a file, or do I need to rewrite the entire file with the new settings.
I would like to do the same process for numerous things, i.e. editing css pages, I just can't find much information about appending the contents of a file, just adding to the file.
Posted: Mon Feb 13, 2006 1:39 pm
by feyd
tiny bit safer:
Code: Select all
$config = array();
foreach($array as $name => $value)
{
$config[] = '$' . $name . ' = ' . var_export($value,true) . ';';
}
echo implode("\n",$config);
I'd suggest using a whitelist on the names, just to make sure..

Re: Edit file contents
Posted: Mon Feb 13, 2006 1:48 pm
by josh
Brian3864 wrote:I want the form to update the file using their settings so that all of the pages with the config file included will have the correct settings. I assume I would need to place each line of the config file into an array, loop through the array until it found the $server, $username, $password at the beginning of the array, change the value for each of those and then rewrite the config file with the new settings. Questions
1. Is that the easiest/best/only way to do this?
easiest? by far
best? opinion
only? of course not
2. Is there a way to just rewrite a couple of lines in a file, or do I need to rewrite the entire file with the new settings.
Generally you would have to re-write the whole file, to avoid this turn to a DBMS; mysql for example
Edit file contents
Posted: Mon Feb 13, 2006 2:21 pm
by Brian3864
Thanks for the responses. I may go with using MySQL, the only problem being the connection as this would need to be modified by hand.