• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

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

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!
  • 4
1 Solution
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";
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.
erikcookAuthor Commented:
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.
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).

see: http://us2.php.net/manual/en/function.include.php
hi erikcook,

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now