1

I am trying to append to a log file, using HTML tables format for each incident. However, there is one difficulty which I am not sure how to solve. Here's a static output of the HTML

<table>
  <tr><td> .... </td>
  </tr>

 <!-- new log entry should enter here -->

  </table>
</html>

The question is how I can quickly insert log entries into the table quickly without messing up the HTML, preferably using the least amount of processing? I know I can load in the entire file, use a templating solution, and rewrite the file but that may be slow (the log files could grow fast quickly!).

So I am looking for the usual open file for append, and somehow write after the last row, but before the tag.

Any suggestions?

PS. I know most browser would render the HMTL if I leave out the tag at the end, but I am curious to see if there could be an ideal solution.

Charles
  • 50,943
  • 13
  • 104
  • 142
Extrakun
  • 19,057
  • 21
  • 82
  • 129

2 Answers2

2

I'd suggest creating the log file with just the table rows, then including it in the file with the needed header and footer.

So you would write to 'log.html', but view 'log.php' which would be:

<html>
  <head>
  </head>
  <body>
    <table>
      <?php include('log.html') ?>
    </table>
  </body>
</html>

Of course, perhaps a better way would be to store the log data without html (in a flat file or database) then generate the view on demand. But for what you're looking for, the above should work.

If it all must be in a static file, you could use

bool ftruncate ( resource $handle , int $size )

to remove the last line of the file. Just make sure you know how long the 'footer' is. Here's some example code that might work for you.

$footer = "</table></html>";
$file = fopen($log_file, 'rw');
ftruncate($file, strlen($footer));
fseek($file, 0, SEEK_END);
fputs($file, $log);
fputs($file, $footer);
fclose($file);

Here's the ftruncate function documentation.

Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
  • Both are good answers; I guess I forgot to mention that I need it as a static .html file as it may be passed thru emails to others. The intent is not really to have a log viewer. – Extrakun Sep 11 '09 at 08:41
  • you can change the .php to .htm by using htaccess: http://www.tutorial5.com/content/view/160/85/ – Natrium Sep 11 '09 at 08:52
  • If static is a must, then the second example should be what you're looking for. But if the server is *serving* the file people e-mail, it shouldn't matter either way, they'll get a complete HTML file. – Tim Lytle Sep 11 '09 at 14:57
1

I'm not really sure what you are asking for, but is it something like this?

<table>
    <tr><td> .... </td></tr>
    <?php
        foreach ($logitems as $item)
        {
            echo "<tr><td>" . $value . "</td></tr>\n";
        }
    ?>
</table>
Natrium
  • 30,772
  • 17
  • 59
  • 73
  • This seems a bit more sane, but it seemed that the question was how to append to a static file. If creating a static file is not necessary, this this is a far better method (IMO) than what I've offered in my answer. – Tim Lytle Sep 11 '09 at 08:36
  • something along the lines of $f = fopen("file.html", "w"); fwrite($f, ); fclose($f); would output the PHP to a static page, no? – Chris Sobolewski Sep 11 '09 at 19:21