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!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hi erikcook,

did you need to know anything else relating to this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.