Jump to content

How to write to a file as configuration


ivytony

Recommended Posts

I would like my website to be configured in a .php file for the website title, meta keywords, description, contact email, etc. I wonder how to write to such a .php file that has config. information in such rows as below:

 

$website_title = 'PHP freaks';

$website_meta_key = 'PHP, solution, freaks';

....

$website_email = 'phpfreaks@gmail';

 

And the second question is how to read the file?

 

Thanks!

Link to comment
Share on other sites

One way to do it would be to make the configuration file without the dollar signs:

file.cfg

website_title=PHP freaks
website_meta_key=PHP, solution, freaks
....
website_email=phpfreaks@gmail

 

Then, in the code:

$conVars = array();
foreach (explode("\n", file_get_contents("file.cfg")) as $value)
{
$temp = explode("=", $value);
$conVars = array_merge($conVars, array($temp[0] => $temp[1]));
}

Then you should have an array of the variables with their values. You can even use the list method if you want to have them as actual variables instead of array keys.

Link to comment
Share on other sites

You could do what you're doing and save it as whatever you want.

 

config.php

<?php
$website_title = 'PHP freaks';
$website_meta_key = 'PHP, solution, freaks';
....
$website_email = 'phpfreaks@gmail';
?>

 

To read this:

 

index.php

<?php
  include ('config.php');

  echo '<html>';
  echo '<head><title>' . $website_title . '</title>'; //sets page title to PHP Freaks
?>

 

 

Personally I would prefer to use globals instead of regular vars.

config_globals.php

<?php
  DEFINE("W_TITLE","PHP Freaks");
  DEFINE("W_META_KW","PHP, solution, freaks");
  ....
  DEFINE("W_EMAIL","phpfreaks@gmail");
?>

 

index.php

<?php
  include('config_globals.php');

  echo '<html>';
  echo '<head><title>' . W_TITLE . '</title>'; //sets page title to PHP Freaks

  //etc..  notice that for global vars you do not use a $ prefix, and you write them like above
?>

Link to comment
Share on other sites

try

<?php
$fileName = 'test.dat';
// write file
$a = fopen($fileName, 'w');
fwrite($a, 'website_title = PHP freaks'."\n");
fwrite($a, 'website_meta_key = PHP, solution, freaks'."\n");
fwrite($a, '[sasa]'."\n");
fwrite($a, 'blah = something'."\n");
fclose($a);

//read file
$a = parse_ini_file($fileName,1);
print_r($a);
//or
echo "<hr />\n";
$a = parse_ini_file($fileName,0);
print_r($a);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.