league/csv for PHP: Complete Guide with Examples

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

Introduction to the league/csv Library for PHP

\n\n

The league/csv library is a powerful and convenient tool for working with CSV (Comma-Separated Values) files in PHP. It provides developers with a simple yet flexible API for reading, writing, processing, and transforming CSV data. Unlike built-in PHP functions (fgetcsv, fputcsv), league/csv offers an object-oriented approach, support for various CSV dialects, handling of BOM (Byte Order Mark), processing of large files through iterators, and many other useful features.

\n\n

This library is part of The League of Extraordinary Packages ecosystem and is considered the de facto standard for working with CSV in PHP. It is actively maintained, has excellent documentation, and is covered by tests.

\n\n

Why Use league/csv?

\n
    \n
  • Simplifies CSV work: Instead of manually managing file pointers and loops, you work with clear objects and methods.
  • \n
  • Security: The library correctly handles character escaping, quotes, and line breaks within fields.
  • \n
  • Flexibility: Support for various delimiters (comma, semicolon, tab), escape characters, and string enclosures.
  • \n
  • Performance: Using generators (yield) allows processing files of any size without loading the entire content into memory.
  • \n
  • Functionality: Built-in methods for conversion to JSON, HTML, XML, sorting, filtering, and column transformation.
  • \n
\n\n

Installing the Library

\n\n

Installing league/csv is done via Composer, the dependency manager for PHP. Make sure Composer is installed on your system.

\n\n

Open a terminal in your project's root directory and run the command:

\n\n
composer require league/csv
\n\n

After successful installation, a vendor folder and composer.json / composer.lock files will appear in your project. To include the library in a PHP script, use the standard Composer autoloader:

\n\n
require 'vendor/autoload.php';\n\nuse League\\Csv\\Reader;\nuse League\\Csv\\Writer;\nuse League\\Csv\\Statement;
\n\n

Quick Start with Code Example

\n\n

Let's create a simple CSV file, write data to it, and then read it back.

\n\n

Example 1: Writing Data to CSV

\n\n
<?php\nrequire 'vendor/autoload.php';\n\nuse League\\Csv\\Writer;\n\n// Create a Writer object\n$writer = Writer::createFromPath('users.csv', 'w+');\n\n// Insert headers\n$writer->insertOne(['ID', 'Name', 'Email']);\n\n// Insert multiple data rows\n$writer->insertAll([\n    [1, 'Ivan Ivanov', 'ivan@example.com'],\n    [2, 'Petr Petrov', 'petr@example.com'],\n    [3, 'Anna Smirnova', 'anna@example.com'],\n]);\n\necho "File users.csv successfully created!";\n?>
\n\n

Example 2: Reading Data from CSV

\n\n
<?php\nrequire 'vendor/autoload.php';\n\nuse League\\Csv\\Reader;\n\n// Create a Reader object\n$reader = Reader::createFromPath('users.csv', 'r');\n$reader->setHeaderOffset(0); // Specify that the first row contains headers\n\n// Get all records as associative arrays\n$records = $reader->getRecords();\n\nforeach ($records as $record) {\n    echo "ID: " . $record['ID'] . ", Name: " . $record['Name'] . ", Email: " . $record['Email'] . "<br>";\n}\n?>
\n\n

Main Methods and Functions of the league/csv Library

\n\n

Let's look at the key methods provided by the library for working with CSV.

\n\n

1. Reader::createFromPath() and Writer::createFromPath()

\n

What it does: Creates a Reader or Writer object for working with a file at the specified path.

\n

Parameters:

\n

Recommendations