Re: Writing to a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jason Carson wrote:
> Hello everybody,
> 
> How would I go about writing stuff to a file but in between the <?php ?>
> tags?
> 
> Example, say I have config.php as follows...
> 
> <?php
> 
> $hostname = "localhost";
> $database = "database";
> $username = "username";
> $password = "password";
> 
> ?>
> 
> How would I go about adding stuff at the end of the file, but before the
> ?> tag?
> 

The way I've done this in the past is to use an array.  Then I can
include the file, change the array and then var_export() to the file:

//config.php
<?php

$config = array (
  'hostname' => 'localhost',
  'database' => 'database',
  'username' => 'username',
  'password' => 'password',
);


//file that does the changing writing
<?php

include('config.php');

$config['database'] = 'otherdb';
$config['username'] = 'jason';
$config['password'] = 'abcdefg123';

$content = "<?php\n".'$config = '.var_export($config, true).";\n?>";
file_put_contents('config.php', $content);

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux