Moja propozycja, może zainspiruje Ciebie.
test_admin_log.php
<?php
$file_name = "../xmloperation/file.txt";
// Można (trzeba?) dopisać zabezpieczenie jak plik file.txt nie istnieje
// coś w stylu if (file_exists($file_name)) { ... } else { $count_lines = 0; }
$lines = file($file_name);
$count_lines = count($lines);
$tr_last_line = "";
$tr_all_lines = "";
if ($count_lines == 0) { // Pusty plik log'ów
$log_line = prepare_log_line('1');
file_put_contents($file_name, $log_line, LOCK_EX);
$tr_last_line = $tr_all_lines = $log_line; // Do demonstracji ;-)
} else {
// $last_line = count($lines) - 1;
// Ponieważ zrobiliśmy w 6 linii kodu count(), możemy
$last_line = $count_lines - 1;
// Odczytujemy ostatni wpis z pliku log - file.txt
$tr_last_line = $lines[$last_line];
// Odczytanie wartości ostatniego ID i utworzenie nowego
$new_id = new_ID($tr_last_line);
// Nowy wpis do pliku log
$new_log_line = prepare_log_line($new_id);
file_put_contents($file_name, $new_log_line, FILE_APPEND | LOCK_EX);
/*
Można jeszcze raz odczyta plik
$lines = file($file_name);
$count_lines = count($lines);
for ($i=0; $i<$count_lines; ++$i) {
$tr_all_lines .= $lines[$i];
}
*/
// ... lub
for ($i=0; $i<$count_lines; ++$i) {
$tr_all_lines .= $lines[$i];
}
// Do wszystkich dodajem ostatnio utworzoną
$tr_all_lines .= $new_log_line;
// ... koniec lub
}
$file_name = basename($file_name);
function prepare_log_line($id) {
$log_line = "<tr><td>%id_loged%</td><td>Logowanie</td><td>Administrator</td><td>%date_loged%</td></tr>";
$log_line = str_replace('%id_loged%', $id, $log_line);
$log_line = str_replace('%date_loged%', date("d.m.Y H:i:s"), $log_line) . PHP_EOL; // :s dodane żeby widać nowe wpisy
return $log_line;
}
function new_ID($input) {
$return = substr($input, strpos($input, "<td>"), strpos($input, "</td>") +1);
$return = (int) strip_tags($return);
return ++$return;
}
?>
<!DOCTYPE html>
<html lang="pl">
<heda>
<meta charset="utf-8">
<style>
body {
margin: 0;
padding: 0;
font: small-caps bold 14px/28px Arial, monospace;
}
table {
margin: 10px;
}
thead {
background-color: black;
color: white;
text-align: center;
}
thead tr:first-child th {
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
thead th {
padding-left: 10px;
padding-right: 10px;
}
tbody td {
text-align: center;
padding-left: 10px;
padding-right: 10px;
}
tbody tr:nth-child(odd) {
background-color: lightgrey;
}
tbody tr:nth-child(even) {
background-color: grey;
}
tbody tr:hover {
background-color: rgba(255, 99, 71, 0.6);
}
tfoot td {
text-align: center;
padding-top: 20px;
}
tfoot button {
box-shadow: 2px 2px 2px black;
-webkit-box-shadow: 2px 2px 2px black;
-moz-box-shadow: 2px 2px 2px black;
font-variant: small-caps;
cursor: pointer;
}
tfoot button:active {
transform: translateY(1px);
-webkit-transform: translateY(1px);
-moz-transform: translateY(1px);
box-shadow: none;
-webkit-box-shadow: none;
-moz-box-shadow: none;
}
.file-name {
padding-left: 10px;
color: yellow;
}
</style>
</heda>
<body>
<table>
<thead>
<tr><th colspan="4">Ostatni wpis z pliku <span class='file-name'><?php echo $file_name; ?></span></th></tr>
<tr><th>ID</th><th>Operacja</th><th>Rola</th><th>Data</th></tr>
</thead>
<tbody>
<?php echo $tr_last_line; ?>
</tbody>
</table>
<table>
<thead>
<tr><th colspan="4">Wszystkie wpisy z pliku <span class='file-name'><?php echo $file_name; ?></span></th></tr>
<tr><th>ID</th><th>Operacja</th><th>Rola</th><th>Data</th></tr>
</thead>
<tbody>
<?php echo $tr_all_lines; ?>
</tbody>
<tfoot>
<tr>
<td colspan="4"><button id="add_new_log">Demo dodaj nowy log</button></td>
</tr>
</tfoot>
</table>
<script>
const addNewLog = document.getElementById("add_new_log");
addNewLog.addEventListener("click", event => {
window.location.reload();
});
</script>
</body>
</html>