3

How do I let users set the database configuration for a new CI install?

My intention is to have a form that allows them to enter the data then either generate a new config file or to set the options in the database.php file.

Is this possible?

1 Answer 1

2

Let's say you want to create a database config file. Here's how I do it:

Create a "dummy" config file by copying a real one, and use values that look something like this:

$db['default']['hostname'] = '__HOSTNAME__';
$db['default']['username'] = '__USERNAME__';
$db['default']['password'] = '__PASSWORD__';
$db['default']['database'] = '__DATABASE__';

Have the user complete the installation form, and verify all the data by testing a database connection. Next, use file_get_contents() to grab the content of the "dummy" config file, use a simple str_replace() to rewrite the values with the ones the user provided, then write the new content to your real config file overwriting the entire thing.

Here's part of the actual code I'm using for creating the file, just to get an idea. Bear in mind that I'm running this on a totally separate CI installation (the "installer"), and this is not a copy/paste example:

// Write the database configuration file
$config = array(
    'hostname',
    'username',
    'password',
    'database',
);

$data = file_get_contents(APPPATH.'config/dummy_database.php');
$file = '../private/config/database.php';

// Oops! Just caught this after posting: str_replace() accepts arrays.
// TODO: Use the array method instead of a loop!
foreach ($config as $item)
{
    $data = str_replace('____'.strtoupper($item).'____', mysql_escape_string($this->data[$item]), $data);
}
if (write_file($file, $data))
{
    $this->message('Database configuration file was written!', 'success');
    @chmod($file, 0644);
    {
        if (is_really_writable($file))
        {
            $this->message('The database configuration is still writable, make sure to set permissions to <code>0644</code>!', 'notice');
        }
    }
}
else
{
    $this->message('Could not write the database configuration file!');
    redirect('step_4');
}

This retains the original copy of the "dummy" config file. You could use the same logic for editing, just read the current config values for anything the user has not changed and do the same string replacement technique.

Sign up to request clarification or add additional context in comments.

7 Comments

So you have one CI installation write the db config for another installation?
Yes, because I'm using an "installer" (separate app), which gets deleted afterwards. You can use this same logic to update your config file though as well, from the same application.
Yeah, I'm getting your concept, but I'm not understanding where you are getting $this->data[$item] from. Also, I can't change permissions on the original database config file to overwrite it. CI is not allowing me to use chmod.
Yeah chmod usually fails for me too, I have to go in via FTP and set the permissions, that's usually step 3 of my installer - checking the permissions before attempting to write the file, this code I gave you is just running that check again. However, I did mention that this was only part of my actual code and not to take it verbatim. I was pretty sure you were asking for the concept because I didn't see any code in your question, you just asked if it was possible. $this->data is an array I use to store the installation details in the installer controller.
So let me see if I understand; You have your user download a package that contains 2 CI apps. Then you use one app to configure the other and then delete the first app? Do you do this all within CI or due you have some outside scripts that you use as well? Because I was thinking of doing something similar, but just writing the installer from scratch. I just keep having problems with the permissions.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.