Reading large files in PHP with SplFileObject

Efficiently load and read from large files in PHP with the built-in SplFileObject class.

fopen() becomes very slow and bogged down once the file size starts getting above a couple of Megabytes. The text file I was using for this example was just under 6MB.

The SplFileObject class is a built-in OOP class for PHP that handles reading large files comfortably and resource-friendly due to its nature of not loading the whole file for reading.

Getting total lines in the file

Getting the total amount of lines in a large file:

<?php
$file_name = "test_for_spl.txt";

$file = new SplFileObject($file_name);
$file->seek($file->getSize());
$total_lines = ($file->key() + 1);

echo $total_lines;

This method is incredibly quicker than looping through every line and doing a count as it utilizes a seek onto the end of the file… aka the last line.

Read a certain line only

<?php
$file_name = "test_for_spl.txt";

$line_to_read = 24865;//Read line 24865

$file = new SplFileObject($file_name);
$file->seek($line_to_read - 1);

echo $file->current();//Output the line

To read just a specified line use seek() with minus 1 because it is zero-based line numbers.

Read multiple lines starting at a certain line

There are 2 ways to read multiple lines from a certain line onwards:

<?php
$file_name = "test_for_spl.txt";

$file = new LimitIterator(
    new SplFileObject($file_name),
    9000,//Start line 9000
    100//Read 100 lines
);

foreach ($file as $line) {
    echo $line . '<br>'; //Line 9000 to 9100
}

or

<?php
$file_name = "test_for_spl.txt";

$start_at = 9000;
$lines_to_read = 100;

$file = new SplFileObject($file_name);

for ($i = 0; $i <= $lines_to_read; $i++) {
    $file->seek($start_at + $i);
    echo $file->current() . '<br>';
}

Unsurprising that seek() makes these quick too.