Re: Updating a single line in a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



For small DBs and where I don't expect very heavy traffic, I simply put everything in an array, where the keys are unique.

Arrays are quite easy to work with for your type of application

Here is a snip I wrote recently
function write_data_file($courses_array){	
	
	$file_str= base64_encode(serialize($courses_array));		//base64 required because of special chars
	
	$fh = fopen(DATA_FILE_FP, "wb");
	if(!$fh) die("<p style=\"color:red\">Code error in update_data_file(), \"DATA_FILE\" could not be opened. Check file system.</p>");
	
if (flock($fh, LOCK_EX)) { // do exclusive lock
	   	fwrite($fh, $file_str);
flock($fh, LOCK_UN); // release lock } else die("<p style=\"color:red\">Code error, could not lock \"DATA_FILE\". Contact tech support.</p>");
	fclose($fh);
	
//No need to restore data dir and file to 755 and 644.
	return;
}//end function	

To get your array back just like it was.

function get_all_courses(){

	$file_str= base64_decode(file_get_contents(DATA_FILE_FP));						//base64 required

	return  unserialize($file_str);													//returns data array
}//end function







smr78 wrote:
Hi,
What is the best method to update a single line in a text file?
I have a file made of identifiers, that is pointed on by a htaccess file and used by a server to give access to a web site.
The file content is like this :
login1:pass1\r\n
login2:pass2\r\n
....
loginn:passn\r\n
loginn1:passn1\r\n
....
lastlogin:lastpass\r\n

This file can be modified in three ways
update a single line when a user updates its profile
delete a single line when the webmaster makes a user inactive
append a line when the webmaster makes a user active

the difficulties are :
there is not the same number of users and lines in this file,
we dont know at which line are the identifiers of a user,
when updating identifiers, we must keep new line characters at the end.

So what are the best functions to use to read and rewrite the file?

file() which puts all the content in an array, including the new line characters? (if so, we need to use array_search() or array_keys() to find where are the identifiers

fread() or file_get_contents() which puts all the content in a string? I tried this way and use eregi_replace() to find where are the identifiers, replace them by new value. But sometimes, I get errors where the new line characters are suppressed between two users identifiers or where an identifier is repeated like this :

loginx:passx\r\n
loginx:passx\r\n
loginn:passnloginn1:passn1\r\n
loginn2:passn2\r\n

Here is my code :
<? php
//reading the actual file content
$length=filesize($filename);
$fp=fopen($filename,"r");
$str=fread($fp,$length);
fclose($fp);

//in case of updating
$str=eregi_replace($oldlogin.":".$oldpass,$newlogin.":".$newpass,$str);
//in case of deleting
$str=eregi_replace($oldlogin.":".$oldpass,"",$str);

//rewriting the file
$handle = fopen($filename,"w+")
fwrite($handle,$str,strlen($str))

//in case of inserting a new user

$str=$newlogin.":".$newpass."\r\n";
$handle = fopen($filename,"a")
fwrite($handle,$str,strlen($str))
?>
I know in "deleting" case, the eregi_replace pattern is so that the new line characters will not be removed, but this is not a problem

I'll try to use file() function and array_keys() and let you know.
Thanks

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux