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
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);
1 Comment
Mohamed Said
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?
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
Mohamed Said
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)?
KARASZI István
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.Mohamed Said
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?
KARASZI István
Then your PHP is older than
5.3.0, just modify the parse_ini_file call and remove the last parameter.KARASZI István
On every request you need to load the ini file. But you need to learn the basics.
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', '' );
file_put_contents()