The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started


Inserting Records

Selecting Records

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress


Author Source Code Latest Version Software License Build Status Total Downloads

CSV is a simple library to ease CSV documents loading as well as writing, selecting and converting CSV records.


Parsing a document

Accessing some records from a given CSV documents.


use League\Csv\Reader;
use League\Csv\Statement;

$csv = Reader::createFromPath('/path/to/your/csv/file.csv');

//set the CSV header offset

//get 25 rows starting from the 11th row
$stmt = (new Statement())

$res = $csv->select($stmt)->fetchAll();

Exporting a database table as a CSV document

Create and download a CSV from a PDOStatement object


use League\Csv\Writer;

//we fetch the info from a DB using a PDO object
$sth = $dbh->prepare(
    "SELECT firstname, lastname, email FROM users LIMIT 200"
//because we don't want to duplicate the data for each row
// PDO::FETCH_NUM could also have been used

//we create the CSV into memory
$csv = Writer::createFromFileObject(new SplTempFileObject());

//we insert the CSV header
$csv->insertOne(['firstname', 'lastname', 'email']);

// The PDOStatement Object implements the Traversable Interface
// that's why Writer::insertAll can directly insert
// the data into the CSV

// Because you are providing the filename you don't have to
// set the HTTP headers Writer::output can
// directly set them for you
// The file is downloadable

Importing a CSV into a database table

Importing CSV records into a database using a PDOStatement object


use League\Csv\Reader;
use League\Csv\Statement;

//We are going to insert some data into the users table
$sth = $dbh->prepare(
    "INSERT INTO users (firstname, lastname, email) VALUES (:firstname, :lastname, :email)"

$csv = Reader::createFromPath('/path/to/your/csv/file.csv');

$stmt = (new Statement())
//by setting the header offset we index all records
//with the header record and remove it from the iteration

foreach ($csv->select($stmt) as $record) {
    //Do not forget to validate your data before inserting it in your database
    $sth->bindValue(':firstname', $row['First Name'], PDO::PARAM_STR);
    $sth->bindValue(':lastname', $row['Last Name'], PDO::PARAM_STR);
    $sth->bindValue(':email', $row['E-mail'], PDO::PARAM_STR);

Converting a UTF-16 CSV file contents to UTF-8

When importing csv files, you don’t know whether the file is encoded with UTF-8, UTF-16 or anything else. The below example tries to determine the encoding and convert to UTF-8 using the iconv extension.


use League\Csv\BOM;
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/your/csv/file.csv');

$input_bom = $reader->getInputBOM();

if ($input_bom === BOM::UTF16_LE || $input_bom === BOM::UTF16_BE) {

foreach ($reader as $record) {
    //all fields from the record are converted into UTF-8 charset

Converting a CSV document into a XML document

Using the provided XMLConverter object you can easily convert a CSV document into a DOMDocument objcet. The below example shows you how to accomplish that.


use League\Csv\XMLConverter;
use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/prenoms.csv')

$converter = (new XMLConverter())
    ->recordElement('record', 'offset')
    ->fieldElement('field', 'name')

$dom = $converter->convert($records);
$dom->formatOutput = true;

echo '<pre>', PHP_EOL;
echo htmlentities($dom->saveXML());
// <?xml version="1.0" encoding="UTF-8"?>
// <csv>
//   <record offset="0">
//     <field name="prenoms">Anaïs</field>
//     <field name="nombre">137</field>
//     <field name="sexe">F</field>
//     <field name="annee">2004</field>
//   </record>
//   ...
//   <record offset="1099">
//     <field name="prenoms">Anaïs</field>
//     <field name="nombre">124</field>
//     <field name="sexe">F</field>
//     <field name="annee">2005</field>
//   </record>
// </csv>