Changing the lines of the text file

Hi,
I have a text file including usernames and passwords in each lines like below:
username1
pass1
username2
pass2

I want users change their passwords via a form. It is ok to go to the line which user's password is included. However when i write to the line using fwrite function, if the user's new pass is longer than previous pass, it overwrites the next line(next user's username line).
Here is the code;

$fp = fopen($file, 'r+');
while ( !feof($fp) ) {
$line = fgets($fp,1024);
$u_name = "username1";
$Find = ereg($u_name, $line, $Array);
if ( ($Find > 0) && ($Array[0]==$Find)) {
$new_pass = "passsssssssss";
fwrite($fp, $new_pass);
echo "ok dude.
";
break;
}
}
fclose($fp);

After runngin this code, text files becomes like this:
username1
passs
ssssssme2 <-- Note here.
pass2

So how can i prevent overwriting the following line. Thanks for replies.

Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion