ADDING TO FILES *THIS NOOB NEEDS HELP*

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
craka
Forum Newbie
Posts: 21
Joined: Thu Jul 11, 2002 9:04 pm

ADDING TO FILES *THIS NOOB NEEDS HELP*

Post 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.
Jose Arce
Forum Commoner
Posts: 37
Joined: Wed May 01, 2002 5:05 pm

Post by Jose Arce »

$fp = fopen ("config.php", "a+"); won't erase your file
craka
Forum Newbie
Posts: 21
Joined: Thu Jul 11, 2002 9:04 pm

Post 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.
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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);
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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..
Post Reply