file writing to tha max

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ramli
Forum Newbie
Posts: 2
Joined: Sat Jan 10, 2004 12:23 pm

file writing to tha max

Post by ramli »

I have a problem i want to add information in a flat file on a specified posision heres a exaple:

----------- Login admin on 18:44:56 - 01.10.2004 -----------

now i want to add information below this like this:

----------- Login admin op 18:44:56 - 01.10.2004 -----------
----------- Has done somthing

and i want to use the date and username as a uniqe key so every thing wil
be recorded to this file regarding actions enz.. using the username and
date as a referance. But there will alsow be new hedears inputted like

----------- Login admin2 op 18:44:56 - 01.10.2004 -----------

so i have to identify the username and date and i alsow must write
to the rule below it so if i input multiple actions at a day for that
username. and at last i must be able to identify it in a login so
the same header will not be written on the same day like this.

----------- Login admin op 18:44:56 - 01.10.2004 -----------
----------- Login admin op 18:44:56 - 01.10.2004 -----------

Can this be done? i had some idees but all didint work so can sombody
pleace help me.........?

Thax in advance
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I guess you will just have to load the file into PHP and split it up into an array (one line per array value) and then use a function to rebuild the array adding any extra info, and then save it to the file again.

Don't you have access to a database?
ramli
Forum Newbie
Posts: 2
Joined: Sat Jan 10, 2004 12:23 pm

yes

Post by ramli »

yeh i have acces to a database but i need the date alsow in a flat file so i can download a log at once and i have limitid mysql database support.
do you have any idee??
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I guess you will just have to load the file into PHP and split it up into an array (one line per array value) and then use a function to rebuild the array adding any extra info, and then save it to the file again.

Here's a quick example...

Code: Select all

<?php

$file = "nameOfTheFile.txt";

$fileOpen = fopen($file, "rb");
$contents = fread($fileOpen, filesize($file));
fclose($fileOpen);

$list = explode("\r\n", $contents); // Will give you an array of the file contents

$insertText = "Some new text";
$insertAfter = "----------- Login admin on 18:44:56 - 01.10.2004 ----------- ";

foreach($list as $key => $value)
{
    $newList[] = $value;
    if($value == $insertAfter)
    {
        $newList[] = $insertText;
    }
}

$newFile = implode("\r\n", $newList);

$openFile = fopen($file, "wb");
fwrite($openFile, $newFile);
fclose($openFile);

?>
I've just type that code as part of the reply so it may or may not work correctly... but it should give you an idea of what to do.

It also presumes that you have one entry per line in the text file.
Post Reply