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
https://forums.phpfreaks.com/topic/26869-writing-to-files/
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
https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-122924
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
https://forums.phpfreaks.com/topic/26869-writing-to-files/#findComment-128219
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.