Page 1 of 1

ADDING TO FILES *THIS NOOB NEEDS HELP*

Posted: Mon Jul 15, 2002 8:15 pm
by craka
Im making an install script for a AOL Instant messanger Guestbook that i made.. I got it to write in the config.php because all that needs to be there is this:

<?
$dbhost = "localhost" ;
$databees = "database" ;
$dbusername = "username" ;
$dbpassword = "pass"
?>

Ok, now on the actual page needed to view it i have to have more code than that. but when i use the same code, the only code i know to do this, it erases everything and just pastes that info into it.

I dont want it to erase everything just add that info to it.

To get it to write in the config.php i am using this script.

$fp = fopen ("config.php", "w+");
fwrite($fp, "<?\n ");
fwrite($fp, $hoststring);
fwrite($fp, $databasestring);
fwrite($fp, $dbusernamestring);
fwrite($fp, $dbpasswordstring);
fwrite($fp, "?>");
fclose($fp);


Im stupid when it comes to php so please help. Thanks.

Posted: Mon Jul 15, 2002 8:29 pm
by Jose Arce
$fp = fopen ("config.php", "a+"); won't erase your file

Posted: Mon Jul 15, 2002 8:45 pm
by craka
Great it workd. Now how do i get it to write that information in a specific spot. Now it is posting it at the bottom of my script.. i need it at the top.

Thanks alot.

Posted: Tue Jul 16, 2002 4:37 pm
by gnu2php
Use "r+" (open a file for reading & writing).

The following places the new data at the beginning of the file:

Code: Select all

$fp = fopen ("config.php", "r+");

$data = fread($fp, filesize("config.php"));

fseek($fp, 0);
ftruncate($fp, 0); // Unsafe (but very rarely)

fwrite($fp, "<?\n ");
fwrite($fp, $hoststring);
fwrite($fp, $databasestring);
fwrite($fp, $dbusernamestring);
fwrite($fp, $dbpasswordstring);
fwrite($fp, "?>");

fwrite($fp, $data); // Write the old data

fclose($fp);

Posted: Tue Jul 16, 2002 5:16 pm
by hob_goblin
i always rather write it at the end, and then using file() and array_reverse() ... i don't know why, it just became a habit. plus half of the time i don't even reverse it, i like the older entries at the top when im parsing it..