PHP read last few lines of the file without copying the entire file to memory -
well know can use this:
<?php $myfile = 'myfile.txt'; $command = "tac $myfile > /tmp/myfilereversed.txt"; exec($command); $currentrow = 0; $numrows = 20; // stops after number of rows $handle = fopen("/tmp/myfilereversed.txt", "r"); while (!feof($handle) && $currentrow <= $numrows) { $currentrow++; $buffer = fgets($handle, 4096); echo $buffer."<br>"; } fclose($handle); ?>
but doesn't copy whole file memory?
a better approach maybe fread()
uses bytes might not approach too.
my file can go around 100mb want it.
if you're doing stuff on command line, why not use tail
directly:
$myfile = 'myfile.txt'; $command = "tail -20 $myfile"; $lines = explode("\n", shell_exec($command));
not tested, should work without php having read whole file.
Comments
Post a Comment