Re: Updating a single line in a file

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

 



Put it all in a database, and write out the file whenever it changes?

Only half-joking.

Virtually all your problems mentioned go away if it's in a database,
and it's not that tricky to set things up so you can write out the
file from the db when you need to.

On Sat, March 18, 2006 4:01 am, 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
>
>


-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
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