Link to home
Start Free TrialLog in
Avatar of dovercomputers
dovercomputers

asked on

Store php code in a variable to write to file

Hi Experts,

I am trying to write an automated setup script so that when user types in details on a form a config file is written to the server.

This is my code:

$handle = fopen($setting['config']['folder'] . $setting['config']['file'], 'w');
	

$input = "<?php
// Generated ".date('F j, Y H:i:s')."

// Database Details

$config['hostname'] = '$db_host';
$config['username'] = '$db_user';
$config['password'] = '$db_pass';
$config['database'] = '$db_name';
$config['table_prefix'] = 'sadmin_';
$config['universal_admin_url'] = '$admin_url';
$config['base_url'] = '$base_ur';
$config['seo_page'] = '$seo_page';
$config['show_errors'] = true;
$config['webmaster_email'] = '$webmaster_email';
$config['webmaster_name'] = '$webmaster_name';
$config['language'] = 'english';
?>
";

fwrite($handle, $input);
fclose($handle);
if (file_exists($setting['config']['folder'] . $setting['config']['file']))
	echo '<h3>Configuration file created!</h3><p>The file <code>'.$setting['config']['folder'] . $setting['config']['file'].'</code> has been created successfully. To continue the installation, please continue the installation. </p><a href="?step=3" class="button" style="float:right;"> <span>Generate Database Tables &rsaquo;</span> </a> ';
else
	echo '<h3>ERROR!</h3><p>Configuration file was not created. Please check the the folder <code>'.$setting['config']['folder'].'</code> is created and the permissions to the  folder are set to <code>777</code>. After checking, click the link button below to regenerate the configuration file again..</p> <a href="?step=1" class="button fail" style="float:left"> <span>&lsaquo; Regenerate Configuration</span> </a>';

Open in new window



But its not working. What I am trying to achieve is the config file shows this inside
<?php

// Generated September 6, 2011 16:00:00

// Database Details

$config['hostname'] = "localhost";
$config['username'] = "jim";
$config['password'] = "password";
$config['database'] = "testdb";
$config['table_prefix'] = "td_";
$config['universal_admin_url'] = "http://www.jimclarke.co.uk/admin";
$config['base_url'] = "http://www.jimclarke.co.uk/";
$config['seo_page'] = "index2.php";
$config['show_errors'] = true;
$config['webmaster_email'] = 'me@example.com';
$config['webmaster_name'] = 'Administrator';
$config['language'] = 'english';
?>

Where am I going wrong?

Thanks
Jim
ASKER CERTIFIED SOLUTION
Avatar of stalhw
stalhw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi
What are you getting now?
Please explain "But its not working."

Is it producing an error or undesirable results?
Avatar of woepwobin
woepwobin

You should escape the dollar signs that do not point to a variable:

...
\$config['hostname'] = '$db_host';
\$config['username'] = '$db_user';
\$config['password'] = '$db_pass';
...

Open in new window

Avatar of dovercomputers

ASKER

Sorry, it is just producing a parsing error. I thought I put that.

I will try escaping the $'s on each line. I just thought that being enclosed within " " they wiouldn't need escaping.

WIll report back shortly.

jim
Thats what it was, I hadn't escaped the $'s at the beginning of each line.

Thank you.