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
how to remove a single line from a text file?
Moderator: General Moderators
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
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
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.
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.