Help with fopen() & file() ?

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
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

Help with fopen() & file() ?

Post by rxsid »

Hi all,
I'm trying to creat a text login file. Problem is that sometimes the previously written user|time is not retained, and sometime the time is truncated and place on a newline as a new entry.
This code should just open the file, build the current array based on appropriate amount of lapsed time, and then put the current user in the file along with the array.

$tnow=date("Gi");
$in=file("TestingIN.txt");

$flag=0;
$cnt = count($in);
for ($x=0;$x < $cnt;$x++) {
list($u_in,$time_in)=split("\|",$in[$x]);

if ("$u" != "$u_in") { //If they dont compare.
if ($time_in>$tnow) {
$tnow1=$tnow+240; //Compensate for different day. Midnight is 000!!
if (($tnow1-$time_in) < 5) { //KEEP if less 5 min.
$old_in[]=$in[$x];
$flag++;
} else {
//Do Nothing
}
} elseif ($time_in < $tnow) {
if (($tnow - $time_in) < 5 ) {
$old_in[]=$in[$x];
$flag++;
} else {
//Do Nothing
}
} else {
//Do Nothing
}
}//END if they dont compare.
}

$newinfo="$u|$tnow\n";

$in2=fopen("TestingIN.txt","w");

$fp=fputs($in2,$newinfo, 40);

for($y=0;$y < $flag;$y++) {
$fp=fputs($in2, $old_in[$y], 40);
}
fclose($in2);

Any ideas why this won't work as stated?

Thanks in advance!
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

$in2=fopen("TestingIN.txt","w");

Using the 'w' parameter completely re-writes the file, removing all previous lines. Look up fopen in the manual for other settings, did you know the manual can be really useful sometimes?
Post Reply