Reading a specific line from a file with PHP

Josh Sherman
2 min read
Software Development PHP

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 crazy 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.

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles