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 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.
<?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.
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.
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…