« Return to Thread: Writing to a file

Re: Writing to a file

by Shawn McKenzie :: Rate this Message:

Reply to Author | View in Thread

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

 « Return to Thread: Writing to a file