Jump to content

Writing to files


markspec87

Recommended Posts

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 :)
Link to comment
Share on other sites

I'd store config.php in ini file format

::cofig.php::
[pre]
host=www.domain.com
user=uname
pwd=pword
db=dbname
[/pre]

Then process like this
[code]
<?php
if (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]
Link to comment
Share on other sites

  • 2 weeks later...
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.