markspec87 Posted November 10, 2006 Share Posted November 10, 2006 Im Currently making a CMS, as i believe it includes a lot of functions and code ill be using time and time again, but ive run into trouble here.I have a file "config.php" which contains[code]$host = "myhost; // db host$user = "myusername"; // db username$pass = "mypassword"; // db password$db = "mydatabase"; // db name[/code]Im looking for some help on editing this file through PHP. i.e if i have a page with database preferences, if these are altered, the new values will be written to this file.If anyone e can help me out that would be greatly appreciated :) Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/ Share on other sites More sharing options...
Barand Posted November 10, 2006 Share Posted November 10, 2006 I'd store config.php in ini file format::cofig.php::[pre]host=www.domain.comuser=unamepwd=pworddb=dbname[/pre]Then process like this[code]<?phpif (isset($_POST['action']) && $_POST['action']=='Update') { $F = fopen('config.php', 'w'); foreach ($_POST['config'] as $k => $v) { fprintf ($F,"%s=%s\r\n", $k, $v); } fclose($F);}$config = parse_ini_file('config.php');?><form method='post'> Host <input type="text" name="config[host]" value="<?php echo $config['host']?>" size="20"><br> User <input type="text" name="config[user]'" value="<?php echo $config['user']?>" size="20"><br> Password <input type="text" name="config[pwd]" value="<?php echo $config['pwd']?>" size="20"><br> DB <input type="text" name="config[db]" value="<?php echo $config['db']?>" size="20"><br> <input type="submit" name="action" value="Update"></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-122924 Share on other sites More sharing options...
markspec87 Posted November 22, 2006 Author Share Posted November 22, 2006 Sorry for bumping this but ive run into problem.After moving host i now only have php4, instead of 5(temporary)is there a way to get this script working on PHP4?[quote]Fatal error: Call to undefined function: fprintf() [/quote]EDIT: according to my sources this is still a function in PHP4? any ideas whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128219 Share on other sites More sharing options...
Barand Posted November 22, 2006 Share Posted November 22, 2006 The only php5 specific code in the script is fprintfReplace the line withfwrite ($F, sprintf("%s=%s\r\n", $k, $v)); Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128223 Share on other sites More sharing options...
markspec87 Posted November 22, 2006 Author Share Posted November 22, 2006 works a treat thank you :)now without being too needy, could you point me how to read out those values? :) :) Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128229 Share on other sites More sharing options...
Barand Posted November 22, 2006 Share Posted November 22, 2006 [code]<?php$config = parse_ini_file('config.php');foreach ($config as $k => $v) { echo "$k : $v <br/>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128232 Share on other sites More sharing options...
markspec87 Posted November 22, 2006 Author Share Posted November 22, 2006 Thanks for that but i need it for use in a script.I.e use the config to read each line into variables.So i can use it to connect to the database.So how would i read those into variables?Sorry for all Questions :) Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128238 Share on other sites More sharing options...
jawapro Posted November 22, 2006 Share Posted November 22, 2006 If you already know WHAT values are in the file, and what order they are in...instead of echoing them - write them into an array.And then just use the appropriate value from the array when you need it.JP Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128244 Share on other sites More sharing options...
Barand Posted November 22, 2006 Share Posted November 22, 2006 $config is an array.[code]$config = parse_ini_file('config.php');$password = $config['pwd'];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128398 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.