Jump to content

[SOLVED] config file


timmah1

Recommended Posts

I'm trying to create a config file through a form.

I can pass all variables with no problem, but when I try to write to the file, I only get some of the info

 

Here is the code

	$file = "config.php";		
	$fh = fopen($file, 'w') or die("can't open file");
	$stringData = "<?\n";
	fwrite($fh, $stringData);
	$stringData = "$dbhost = '".$host."';\n";
	fwrite($fh, $stringData);
	$stringData = "$dbuser = '".$user."';\n";
	fwrite($fh, $stringData);
	$stringData = "$dbpassword = '".$pass."';\n";
	fwrite($fh, $stringData);
	$stringData = "$dbdatabase = '".$database."';\n";
	fwrite($fh, $stringData);
	$stringData = "?>\n";
	fwrite($fh, $stringData);
	fclose($fh);

 

On the config file, it only shows up like this

= 'host';
= 'user';
etc..

 

How can I make it write $dbhost = MyVariable

and so forth?

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/133547-solved-config-file/
Share on other sites

variables are evaluated inside double quotes...so use single quotes or escape it with a slash in front. but, the easier way to do this is with a $config array variable:

 

<?php
  //Build your array so it looks like
  $config = array(
    'dbhost' => $host,
    'dbuser' => $user,
    'dbpassword' => $pass,
    'dbdatabase' => $database,
  );
  //Now save it
  $file = "config.php";
  file_put_contents('<? $config = ' . var_export($config,true) . '; ?>');
?>

 

edit: it's not saving right, that last line should be this, without the new lines:

file_put_contents(
'<? $config = ' . 
var_export($config,true) . 
'; ?>'
);

Link to comment
https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694657
Share on other sites

I have tried this

  $file = "config.php";
  file_put_contents('<? $file = ' . var_export($config,true) . '; ?>');

 

I even tried replacing the = with , and the same error

Warning: file_put_contents() expects at least 2 parameters, 1 given on line 106

Link to comment
https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694689
Share on other sites

int file_put_contents  ( string $filename  , mixed $data  [, int $flags  [, resource $context  ]] )

 

file_put_contents

 

  $file = "config.php";
  file_put_contents($file, '<? $file = ' . var_export($config,true) . '; ?>');

 

Should fix you up.

Link to comment
https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694703
Share on other sites

The script should be....

 

$file = "config.php";
file_put_contents($file, '<?php $config = ' . var_export($config,true) . '; ?>');

 

Which will give you a config file like....

 

$config = array (
  '$dbhost' => 'localhost',
  '$dbuser' => 'xxxx',
  '$dbpassword' => 'xxxx',
  '$dbdatabase' => 'xxxx',
);

Link to comment
https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694733
Share on other sites

I was trying to do a simple output

 

$dbhost = "$hostname ";
$dbuser = "$username";
$dbpassword = "$password";
$dbdatabase = "$database";

 

I have a form where the user enters their database info and clicks on submit, and I want it to write to the config.php with the variables

 

$hostname = trim($_POST['hostname']);
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$database = trim($_POST['database']);

Link to comment
https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694749
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.