Re: Writing to a file

From: Date: Fri, 03 Jul 2009 16:13:23 +0000
Subject: Re: Writing to a file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-294868@lists.php.net to get a copy of this message
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

« previous php.general (#294868) next »