14

What I'm trying to do is to make an installation file where the user enters the database, username, password, and host as a first step in a php system installation.

4
  • 3
    Reference: PHP: Filesystem functions Commented Jun 20, 2011 at 13:13
  • 1
    file_put_contents() Commented Jun 20, 2011 at 13:13
  • php.net/function.fwrite Commented Jun 20, 2011 at 13:13
  • 1
    @yi_H - Can you provide a synopsis or usage example, preferably one relevant to the OP's question? Commented Jun 21, 2011 at 3:18

3 Answers 3

19

It is same as you are creating other files but just add the extension .php

$fp=fopen('filename.php','w');
fwrite($fp, 'data to be written');
fclose($fp);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but how to use form inputs in writing that .php page: Example, when the user enters the database name, it should be used in the config.php file that we want to create. Is it possible to use $database = $_POST['database']; inside the fwrite function?
13

It's easy. Simply write a file with php extension as others mentioned.

But I would rather write an ini file for configuration data and load them later with parse_ini_file.

Update: Here is an example to do that:

<?php
$config = array(
    "database" => "test",
    "user"     => "testUser"
);

function writeConfig( $filename, $config ) {
    $fh = fopen($filename, "w");
    if (!is_resource($fh)) {
        return false;
    }
    foreach ($config as $key => $value) {
        fwrite($fh, sprintf("%s = %s\n", $key, $value));
    }
    fclose($fh);

    return true;
}

function readConfig( $filename ) {
    return parse_ini_file($filename, false, INI_SCANNER_NORMAL);
}

var_dump(writeConfig("test.ini", $config));
var_dump(readConfig("test.ini"));

5 Comments

Would you please tell me more about that? How can I create an installer to create a config file using the (write an ini file)?
I added an example, but it's just a simple example without too much error handling and if you have parameters with spaces you need to escape that at the sprintf.
I tried that, but an error appeared: Warning: Wrong parameter count for parse_ini_file() . Also, how can I use a form input to create the config file, and how to read the config file in order to be able to connect to the database on each page on my website?
Then your PHP is older than 5.3.0, just modify the parse_ini_file call and remove the last parameter.
On every request you need to load the ini file. But you need to learn the basics.
2

I know this is an old topic but I have a solution:

installer.php:

<?php

class Installer {

    public $options;

    public function __construct( $arr=array() ){

        $this->options = $arr;

    }

    public function put( $key, $value ){

        $this->options[$key] = $value;

    }

    public function convertOptions(){

        $arr = $this->options;
        $data = "<?php\n\n";

        foreach( $arr as $key => $value ){

            $data .= "define( '" . $key . "', '" . $value . "' );";
            $data .= "\n\n";

        }

        return $data;

    }

    public function install( $filename, $dir='' ){

        if( file_put_contents( $dir . $filename, $this->convertOptions() ) ){

            return true;

        }

        return false;

    }

}

anotherFile.php:


require_once installer.php;

$installer = new Installer();

$installer->put( 'DB_NAME', 'example' );
$installer->put( 'DB_USER', 'root' );
$installer->put( 'DB_PASS', '' );

$installer->install( 'config.php' );

created config.php:

<?php

define( 'DB_NAME', 'example' );

define( 'DB_USER', 'root' );

define( 'DB_PASS', '' );

Comments

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.