Development

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.

 

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago