Solved

Store php code in a variable to write to file

Posted on 2011-09-06
6
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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