Page 1 of 1

Automating a flatfile maintenance routine

Posted: Mon May 18, 2009 8:43 am
by Peter_LT
Hello Folks,

For some while now I've been very successfully using the kloth.net
bad robots trap. It uses a flatfile reference called blacklist.dat.

Every couple of months I manually trim this file, usually when it has
grown to around 200 lines or more. I take out the first 50 entries,
then save the file. Is there a way of automating this procedure?

The blacklist.dat file is written to by activation of the trap, so what
I'd like to be able to do is...

test whether there are 200 entries
if so, read in the file starting at line 50
then save the trimmed file.

I'm reasonably OK with PHP to generate HTML content, but a complete
duffer when it comes to file handling. Any help you give will be be very
gratefully received. Thanks in advance.

Re: Automating a flatfile maintenance routine

Posted: Thu May 21, 2009 9:13 am
by Peter_LT
Hello Folks,

Progress so far...

Code: Select all

 
        $filearray = file($filename);
        $c  = count($filearray);
        if ($c > $limit) {
            $fp = fopen($filename,"w") or die ("Error opening file ...");
            for($i=($c < 150 ? 0 : $c-150);$i < $c;++$i) {
                fputs($fp,$filearray[$i]);
            }
            fwrite($fp,"$REMOTE_ADDR                       $datum \n");
            fclose($fp);
        } else {
        $fp = fopen($filename,"a+") or die ("Error opening file ...");
        fwrite($fp,"$REMOTE_ADDR \n");
        fclose($fp);
        }
This is the PHP code that I've added. Will this work reliably?