Jump to content

Updating Flat Files, using fopen, fwrite, and fclose techniques.


kratsg

Recommended Posts

Here is a function I wrote that will handle my password changes for me. There's the regular form of putting in your current password, and typing your new password in twice. This form will take the username, current password, and the two new passwords typed in and process the information. $admin is basically an administrative override if the administrator needs to change the password for a user.

 

<?php
function change_pass($change_user,$old_pass,$new_pass1,$new_pass2,$admin){
//we will find the user, and check for password matching, change and blah
if($admin){//true=admin changing someone's pass, meaning we don't check password
	$confirm = true;//default it as true
} else {//false=user changing own password, meaning we check
	$confirm = check_pass($change_user,$old_pass);
}
if($confirm){//we passed the check
	if($new_pass1 == $new_pass2){//they typed the new password correctly both times
		$file = fopen(".htpasswd",'r');
		while($line = fgets($file)){//while we can still read the file
			$line = trim($line);
			list($users,$passes) = explode(':',$line);
			if($change_user == $users){//we found the user, change the password
				$passes = encrypt_pass($new_pass1);
			}
			$contents[] = "$users:$passes\n";
		}
		fclose($file);

		//we've read the file, replaced what needed to be replaced, let's re-open and write
		$file = fopen(".htpasswd",'w');
		foreach($contents as $data){
			fwrite($file,$data);
		}
		fclose($file);
		return true;
	} else {return false;}
} else {return false;}
}
?>

 

I've commented it enough so that you should be able to follow along. The script basically does this, "Let's read the file, and store it into an array. If I find the username who's password I have to change, I will save the new password into the array, rather than the old password that I read from the file. Then, I'm done reading, I close the file, I re-open it, clearing off everything, and re-write it line by line for what was in the array I made."

 

What I want to do (if possible) is to be able to skip through it, is to read the file and update it as I go. So instead of reading, remembering the information, and then re-writing the whole file, is there a way I can read and replace content as I go?

 

So I go line by line, if I find something to change, I change it and proceed with the rest of reading the file...

Link to comment
Share on other sites

In all other languages I've coded in, overwriting was the easiest and fastest way.  I don't do much flat file manipulation in PHP so I can't comment on that.  The only way I've found to replace lines was to use [keys] and then replace their values in the .txt file (.ini files really, but that is also a .txt file).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.