I've explored all over the internet to find a solution; but all of them are neglecting an important issue. The best solution was in Stack Overflow as
$file = new SplFileObject('longFile.txt');
$fileIterator = new LimitIterator($file, 1000, 2000);
foreach($fileIterator as $line) {
echo $line, PHP_EOL;
}
But like other approaches, this needs to read from the beginning of the file to reach the offset line. Usually, it is negligible; but for large files (say millions of line), this significantly slow down the process. The time increases monotonically by the increase of the offset. If you put the offset at millions, the process time will be few seconds.
In databases (like mysql), we index the table to read a row without walking through the whole database. Is there to do such thing with the file key (line number)? I wonder how flat file databases like SQLite and Berkeley DB do index their tables.
The conceptual problem here is that files are just strings of characters, some of those characters denote ends of lines. Because of that, it is impossible to know where lines begin and end without reading the file first.
If a file is to be read constantly, you scan the file first and record the offsets for the lines into some kind of index and use fseek() and fread() to read exactly the lines you want.
As you mentioned, databases can do a similar job for you so, instead of creating, essentially, your own database, you could read the file line–by–line and insert those lines in database with some field storing the line number and then get the lines you want with a query.