Solved

Store php code in a variable to write to file

Posted on 2011-09-06
6
301 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:dovercomputers
6 Comments
 
LVL 8

Accepted Solution

by:
stalhw earned 500 total points
ID: 36489194
you probably need to escape $config
like \$config
and if you want double quotes you need to escape them too:

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

// Database Details

\$config['hostname'] = \"$db_host\";


But you are not saying what's not working, are you getting error messages? is the file created?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36489204
What are you getting now?
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 36489217
Please explain "But its not working."

Is it producing an error or undesirable results?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:woepwobin
ID: 36489226
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

0
 

Author Comment

by:dovercomputers
ID: 36489258
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
0
 

Author Closing Comment

by:dovercomputers
ID: 36489316
Thats what it was, I hadn't escaped the $'s at the beginning of each line.

Thank you.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question