Working with CSV in PHP: SplFileObject Library and Built-in Functions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is CSV and why work with it in PHP?

CSV (Comma-Separated Values) is a format for storing tabular data, where rows are separated by line breaks and columns by commas or other delimiters. In PHP, working with CSV is necessary for importing/exporting data from Excel, Google Sheets, CRM systems, as well as for processing logs and exchanging data between services. The standard PHP library provides powerful tools for reading and writing CSV without installing additional modules.

Main functions for working with CSV

PHP has built-in functions: fgetcsv() — reading a CSV line from a file, fputcsv() — writing an array in CSV format, as well as the SplFileObject class, which simplifies iteration over CSV files. For working with memory, you can use str_getcsv() to parse a string without a file.

Installation

No additional installation is required, as the functions are part of the PHP core. To work with CSV, simply enable support for file operations (usually enabled by default).

Main functions and their usage

  • fgetcsv($handle, $length, $delimiter, $enclosure, $escape) — reads a line from an open file and returns an array of fields.
  • fputcsv($handle, $fields, $delimiter, $enclosure, $escape) — writes an array as a CSV line to a file.
  • str_getcsv($input, $delimiter, $enclosure, $escape) — parses a CSV string without a file.
  • SplFileObject::fgetcsv() — a method for reading CSV in an object-oriented style.

PHP code example

<?php// Reading a CSV file$filename = 'data.csv';if (($handle = fopen($filename, 'r')) !== false) {    while (($data = fgetcsv($handle, 1000, ',')) !== false) {        echo 'Name: ' . $data[0] . ', Email: ' . $data[1] . '<br>';    }    fclose($handle);}

// Writing to CSV$list = [ ['Ivan', 'ivan@example.com'], ['Maria', 'maria@example.com']];$handle = fopen('output.csv', 'w');foreach ($list as $fields) { fputcsv($handle, $fields);}fclose($handle);

// Using SplFileObject$file = new SplFileObject('data.csv');$file->setFlags(SplFileObject::READ_CSV);foreach ($file as $row) { print_r($row);}?>

When to use

The library is suitable for: importing products from Excel into an online store, exporting reports, processing contacts from CRM, loading data from Google Sheets via CSV export, as well as for migrating data between systems. For large files, it is recommended to use SplFileObject iterators or line-by-line reading to avoid memory overflow.

Conclusion

PHP's built-in tools for working with CSV are a reliable and fast solution for most tasks. They do not require installing additional libraries, support various delimiters and escaping, and integrate easily with databases and web forms. If you need to process CSV in PHP, start with fgetcsv() and fputcsv() — this is the de facto standard.

Recommendations