One last PHP Question! Find Lines of a TXT and cut/paste them into an "archive.txt"

Posted on 2004-11-18
Last Modified: 2008-03-06
I currently have a wonderful IP tracking script that outputs information to a txt file in the following format... (Thanks to Diablo84)

Ip Address: - Date: November 18, 2004 - Time: 11:58
Ip Address: - Date: November 18, 2004 - Time: 11:59
Ip Address: - Date: November 18, 2004 - Time: 12:00

I need a script that will sit on a webpage and appear as a simple Text/Image link.

When activated, the script will search the first text file (ip.txt) and will cut all lines except the last 100 and paste them into a secondary file called archive.txt.
Archive.txt can have infinate entries...

a little explanation here:

The script pastes entries from top to bottom, so the bottom lines will be the most recient activity. Therefore I need all lines cut except the last 100, if there are fewer than 100 lines in ip.txt the script returns a text box, "There are less than 100 Entries in the IP log, no entries have been archived".
If the lines were successfully "archived" a box will apear reading "The Security Log was successfully archived".

I realise I am asking for a full script so I am putting the points high, but it is primarily for my own learning experience... so thanks very much in advance!
Question by:erikcook
    LVL 27

    Expert Comment

    one method:

    $lines = file("ip.txt");
    $count = count($lines);

    if ($count <= 100) {
     echo "There are less than 100 Entries in the IP log, no entries have been archived";
    else {
     $archive = array_slice($lines,0,$count-100);
     $restore = array_slice($lines,$count-100);
     if (!$handle = fopen("archive.txt","a")) die("Cannot open file");
     if (fwrite($handle, implode("",$archive)) === false) die("Cannot write to file");
     if (!$handle = fopen("ip.txt","w")) die("Cannot open file");
     if (fwrite($handle, implode("",$restore)) === false) die("Cannot write to file");
     echo "The Security Log was successfully archived";
    LVL 27

    Expert Comment

    Not as complicated as you may have expected.

    Basically we read each line of the ip file into an array using the file function (then do the line count check to make sure there are more then 100 entries) then we take two slices from the array. The first slice is the archive slice (all of the array items - 100 - ie. all except the most recent 100 entries), the second the the ip current 100 slice, everything not included in the first slice. These two sets of data are then written to file, the archive items are added to the end of the archive file and the 100 current entries are restored  to ip.txt.

    Author Comment

    Thats awesome. . you're right I figured it would be alot more difficult.


    Is it possible to use SSI and PHP on the same pages? I have SSI Includes that I use and need them on the same page as my php scripts.
    LVL 27

    Accepted Solution

    In php you can use the include function, eg: include("file.txt");

    or require if it is critical for the script (it will terminate the script if the file can't be included).

    LVL 27

    Expert Comment

    hi erikcook,

    did you need to know anything else relating to this question?

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now