As I’ve been building a system that is driven primarily by CSV files (because
it’s easier to use Google Sheets than it is to build out an admin section) I’ve
had to get creative.
Recently I found myself working with a CSV file that’s around of 3000 lines of
data that’s close to 350k in size. Nothing too out of control, but it has the potential to get larger and I wanted to read the data to an indexed array. Since it’s CSV I have to process it back out to an array as well.
I explored a few options to reduce the time to load the data and would have been
caching it so I wouldn’t have to reprocess it often but I wanted more. I wanted
to read a specific line from the file.
Something that’s eluded me since it’s introduction in PHP 5.1.0 is
SplFileObject
. The generic description is that it’s a class that offers an
object-oriented interface for a file.
The SplFileObject
adds is a way to seek out a specific line in a file and read
it on. Better still, it allows you to convert the data from CSV to an array
right from the object.
The usage is pretty simple. Let’s say you have a 3000 line file like I do, and
you want to grab the 1500th line:
<?php
$spl = new SplFileObject('/path/to/huge/file.txt'
$spl->seek(1499 // Zero-based numbering
echo 'The 1500th line is: ' . $spl->current
As mentioned, if your file is in CSV format, you can convert the CSV to an array
with ease:
<?php
$spl = new SplFileObject('/path/to/huge/file.csv'
$spl->seek(1499 // Zero-based numbering
echo 'The 1500th line is: ' . "n"
print_r($spl->fgetcsv());
There’s a ton more to SplFileObject
that you may be interested in exploring.
You can use it to loop through all the files of a file as well as handle writing
to a file. Moving forward I plan to use this where I would have normally used
fopen
as it’s significantly easier to use.