How to create .php files using php?

Viewed 32170

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.

3 Answers

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', '' );
Related