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
file writing to tha max
Moderator: General Moderators
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...
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.
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);
?>It also presumes that you have one entry per line in the text file.