How to save memory when reading a file in Php?
Solution 1:
Try SplFileObject
echo memory_get_usage(), PHP_EOL; // 333200
$file = new SplFileObject('bible.txt'); // 996kb
$file->seek(5000); // jump to line 5000 (zero-based)
echo $file->current(), PHP_EOL; // output current line
echo memory_get_usage(), PHP_EOL; // 342984 vs 3319864 when using file()
For outputting the current line, you can either use current()
or just echo $file
. I find it clearer to use the method though. You can also use fgets()
, but that would get the next line.
Of course, you only need the middle three lines. I've added the memory_get_usage
calls just to prove this approach does eat almost no memory.
Solution 2:
Unless you know the offset of the line, you will need to read every line up to that point. You can just throw away the old lines (that you don't want) by looping through the file with something like fgets()
. (EDIT: Rather than fgets()
, I would suggest @Gordon's solution)
Possibly a better solution would be to use a database, as the database engine will do the grunt work of storing the strings and allow you to (very efficiently) get a certain "line" (It wouldn't be a line but a record with an numeric ID, however it amounts to the same thing) without having to read the records before it.