Page 1 of 1

how to remove a single line from a text file?

Posted: Thu May 22, 2003 12:49 pm
by jchibbra
Hi guys,

my question is really simple.
How do I remove 1 line from a text file.

example:
I have a .htpasswd file with several entries.
I would like to remove the line which contain the login: bob

How do I do that when I don't want to put the file in an array, remove the entry from the array and then re-write the file with the new array content.

The reason is that I want to do something really fast. So if you have a solution which is fast you are welcome.

Is someone know how php manage file access because I was wondering what could happen if at the same time I open the .htpasswd file to remove an entry and another function try to create an entry in the .htpasswd.

Tks in advance

Posted: Thu May 22, 2003 12:58 pm
by liljester
may be faster if you read the entire file to a single variable (use fread()), and perform a str_replace("login: bob", "", $whole_file), then rewrite the file?

Posted: Thu May 22, 2003 4:02 pm
by jchibbra
I am not sure it is faster because I have to open the file twice
The following code doesn't work:
$log = toto;

if (!$fd = fopen(".htpasswd", 'w+')) {
print "Cannot open .htpasswd";
exit;
}
$str_f = fread($fd, filesize(".htpasswd"));
echo 'before:<br>'.$str_f.'<br>';
$str_f = ereg_replace("$log:.{14}", "", $str_f);
echo '<br>after:<br>'.$str_f.'<br>';
if (!fwrite($fd, $str_f)) {
print "Cannot write to file .htpasswd";
exit;
}

The result is that when I do 'echo before' and 'echo after', the variable $str_f is always empty. the only way to avoid that is to put 'r+' instead of 'w+' but the .htpasswd has all the login:pass doubled execpt the matched line which is only once.

That is why I have to do:
if (!$fd = fopen(".htpasswd", 'r+')) {
print "Cannot open .htpasswd";
exit;
}
$str_f = fread($fd, filesize(".htpasswd"));
fclose($fd);

if (!$fd = fopen(".htpasswd", 'w')) {
print "Cannot open .htpasswd";
exit;
}
echo 'before:<br>'.$str_f.'<br>';
$str_f = ereg_replace("$log:.{14}", "", $str_f);
echo '<br>after:<br>'.$str_f.'<br>';
if (!fwrite($fd, $str_f)) {
print "Cannot write to file .htpasswd_g";
exit;
}
fclose($fd);

Do you really think it is faster if I have to open the file twice??
is someone as another way to do it faster?

the .htpasswd looks like:
----------------------------
bob:1$4qKVfBctVNg
titi:1$X3z1Dh4u8xo
toto:1$DbF0lKVrmDg
sloppy:1$zXLrM9X7iig
mike:1$Fqyom7mphtw
ice:1$FilG035A.xC

Posted: Thu May 22, 2003 4:39 pm
by liljester
if you file is only 6 lines, i dont think speed would be a concern....

Posted: Thu May 22, 2003 4:46 pm
by jchibbra
Yes that is true.
However this is only an example and the web site is supposed to grow up and then increase the size of the .htpasswd.

So if someone can provide a solution which is more efficent.

Thanks in advance.

Posted: Thu May 22, 2003 6:14 pm
by mikusan
.htaccess give more control to webmasters, and .passwd is the file that is better suited to store passwords, if your site is going to grow then may i suggest database? that way you can md5 all your passwords along with the username his favourite pet and eyecolour :P.

Posted: Fri May 23, 2003 12:18 pm
by jchibbra
I am using mysql to store all the information however I cannot use the mod_auth_mysql module because I cannot touch their system for the moment that is why I am using .htpasswd and .htaccess.
I mean I have to recompile apache in order to enable the module mod_auth_mysql but currently I cannot.

So I still have the same question. Does someone know how to do it efficiently. :wink: