Automating a flatfile maintenance routine

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Peter_LT
Forum Newbie
Posts: 5
Joined: Mon May 18, 2009 8:21 am

Automating a flatfile maintenance routine

Post 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.
Peter_LT
Forum Newbie
Posts: 5
Joined: Mon May 18, 2009 8:21 am

Re: Automating a flatfile maintenance routine

Post 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?
Last edited by onion2k on Thu May 21, 2009 9:17 am, edited 1 time in total.
Reason: Set your signature if you want a signature. Don't add pointless links to posts.
Post Reply