0

I've been searching for solutions here for a long time now, and i haven't been able to find any so far

what i need is a way to read the last 10 or so lines of a text file, which continually updates every second

i've found some possible codes in php, but then i wont be able to update the file every second.

how do i get only the last 10 lines of a text file using some sort of javascript?

Daniel Holst
  • 139
  • 10

3 Answers3

0

Try using array_slice, which will return a part of an array. In this case you want it to return the last 15 lines of the array,

$filearr = file("filename");
$lastlines = array_slice($filearr,-10);

You can change -10 (-10,-15 any value you want).

Hope it works!

Kautil
  • 1,321
  • 9
  • 13
0

Hmmm... maybe so, for large files:

$fileName = '[path_to_file]';
$file = new \SplFileObject($fileName);
$file->seek($file->getSize());

$lines = $file->key() - 1;

$file->setFlags(\SplFileObject::READ_CSV);

$counter = 0;
$records = 100;
$seek = $lines - $records;

$file->seek($seek);

$return = array();
while ($counter++ < $records) {
    try {
        $line = $file->fgets();
        $return[] = $line;
    } catch (\Exception $e) {
    }
}
mkjasinski
  • 3,115
  • 2
  • 22
  • 21
0

Didn't find the correct answer here, so i started a new question where i'm more specific with what i want

Reading ONLY the last 50 lines of a large text file

Community
  • 1
  • 1
Daniel Holst
  • 139
  • 10
  • You should not have reposted. Instead, you should have edited the question to 1) make more clear what you want and 2) generate some activity (the editing) so it will reappear on the front page. If you would have done that you wouldn't have to post another duplicate. – 11684 Mar 17 '13 at 20:50