jdavis Posted November 16, 2007 Share Posted November 16, 2007 I am currently building a PHP app, and some of the configuration is stored in a 'config.php' file which is created during the install. After install I would like for the users to update these values separately. for example: line 5 of config.php is: $language = 'english'; and this may need updating from the admin screen (using results from a drop down list of languages), without touching the rest of the file. Is there any way to locate and update a single line of the config file in my php script? thanks, Jonathan Quote Link to comment Share on other sites More sharing options...
revraz Posted November 16, 2007 Share Posted November 16, 2007 I haven't done of that type of coding in PHP, but have in VB, and what I normally do is load in all the variables into some type of Edit form, and then make your changes, then rewrite then entire file rather than just updating one line. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 16, 2007 Share Posted November 16, 2007 obviously sql makes a stronger choice here, but if you must go flat file you need to use file_get_contents then from the contents make 100% sure each line is a new variables and explode the file at the \n then simply find your translations between the indexial array and your associtive array that you used i.e <?php $file = "config.php" $data = file_get_contents($file); $data = explode("\n",$data); print_r($data); ?> Now i'll assume you will have some sort of html form, maxamizing use of selects as this reduces the need for data cleansing. On the process page you will need to do the whole process in reverse, rewriting the whole file so you will either A need to carry across every variable in post (Maybe not your best option) or reget the contents with file_get_contents on the processor page. then once you get that depending on if you are on 4 or 5 you will use file_put_contents or some varient. Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 16, 2007 Share Posted November 16, 2007 My idea: 1. Get config.php file into string variable using file_get_contents 2. Perform a preg_replace on the string 3. Write the string to file using file_put_contents Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 16, 2007 Share Posted November 16, 2007 Assume that the file looks like the following.... $variable='somevalue'; $variable2='anothervalue'; $vars='values'; Then the following function can be used: function edit_line($variable,$replace_val){ $file = fopen("textfile.txt","r+"); while($line = fgets($file)){//go line by line $line = trim($line); list($var,$val) = explode("=",$line);//explode each line if($var == '$'.$variable){//we found corresponding variable $val = $replace_val; } $contents[] = $var; $contents[]= $val; $data .= implode("=",$contents).";\n";//stick it back into a line again } //we've looped through the file and done the replacing if needed, let's update the file fwrite($file,$data); fclose($file); } Run the function: edit_line("language","'value'");//note that {value} has single quotes surrounding it... Quote Link to comment Share on other sites More sharing options...
jdavis Posted November 19, 2007 Author Share Posted November 19, 2007 Thanks for all the great advice, I have created an install script that writes the values to the config.php, and then created an admin screen by loading all the current variables back into the form and re-writing the file back again. Not the best way I'll admit, and it will probably move to MySQL in the future, but it works great for what I need! thanks again, Jonathan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.