timmah1 Posted November 20, 2008 Share Posted November 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/ Share on other sites More sharing options...
rhodesa Posted November 20, 2008 Share Posted November 20, 2008 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) . '; ?>' ); Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694657 Share on other sites More sharing options...
timmah1 Posted November 20, 2008 Author Share Posted November 20, 2008 I tried doing what you said, I get this error Warning: file_put_contents() expects at least 2 parameters, 1 given on line 106 Line 106 is this file_put_contents('<?php $config = '.var_export($config,true). '; ?>'); Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694661 Share on other sites More sharing options...
rhodesa Posted November 20, 2008 Share Posted November 20, 2008 duh...first argument should be $file Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694668 Share on other sites More sharing options...
timmah1 Posted November 20, 2008 Author Share Posted November 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694689 Share on other sites More sharing options...
timmah1 Posted November 20, 2008 Author Share Posted November 20, 2008 I got this to print the array, but it just printed Array on the config file. Here is code that I got to work file_put_contents("<? $file = " . var_export($config) . "; ?>",true); Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694702 Share on other sites More sharing options...
premiso Posted November 20, 2008 Share Posted November 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694703 Share on other sites More sharing options...
timmah1 Posted November 20, 2008 Author Share Posted November 20, 2008 Works perfect premiso. Thank you rhodesa for your help Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694710 Share on other sites More sharing options...
timmah1 Posted November 20, 2008 Author Share Posted November 20, 2008 OK, This script rights a config.php file like so config.php = array ( '$dbhost' => 'localhost', '$dbuser' => 'xxxx', '$dbpassword' => 'xxxx', '$dbdatabase' => 'xxxx', ); How do I use this to connect to the database? Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694731 Share on other sites More sharing options...
trq Posted November 20, 2008 Share Posted November 20, 2008 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', ); Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694733 Share on other sites More sharing options...
trq Posted November 20, 2008 Share Posted November 20, 2008 Actaully, I didn't even look at the output. That wont work for you at all. Do you want an array within your config file or simple variable (I would recmmend constants) ? Post you actual code. Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694734 Share on other sites More sharing options...
timmah1 Posted November 20, 2008 Author Share Posted November 20, 2008 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']); Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694749 Share on other sites More sharing options...
trq Posted November 20, 2008 Share Posted November 20, 2008 <?php $data = " <?php \$dbhost = \"$hostname\"; \$dbuser = \"$username\"; \$dbpassword = \"$password\"; \$dbdatabase = \"$database\"; ?>"; file_put_contents('config.php', $data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694751 Share on other sites More sharing options...
timmah1 Posted November 20, 2008 Author Share Posted November 20, 2008 Once again thorpe, you have blessed me with your knowledge. I thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/133547-solved-config-file/#findComment-694754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.