The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections

Inserting Records

Selecting Records

Converting Records

Upgrading Guide

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

Document loading

Connection type

Accessing the CSV document is done using one of the following class:

Both classes extend the League\Csv\AbstractCsv class and as such share the following features:

OS specificity

If your CSV document was created or is read on a Macintosh computer, add the following lines before using the library to help PHP detect line ending in Mac OS X.

<?php

if (!ini_get("auto_detect_line_endings")) {
    ini_set("auto_detect_line_endings", '1');
}

//the rest of the code continues here...

Instantiation

<?php

public static AbstractCsv::createFromString(string $str): self
public static AbstractCsv::createFromPath(string $path, string $open_mode = 'r+'): self
public static AbstractCsv::createFromStream(resource $stream): self
public static AbstractCsv::createFromFileObject(SplFileObject $obj): self

Because CSV documents come in different forms we use named constructors to offer several ways to load them.

Create from a string

<?php

public static AbstractCsv::createFromString(string $str): self

Creates a new object from a given string.

<?php

use League\Csv\Reader;
use League\Csv\Writer;

$reader = Reader::createFromString('john,doe,john.doe@example.com');
$writer = Writer::createFromString('john,doe,john.doe@example.com');

Create from a file path

<?php

public static AbstractCsv::createFromPath(string $path, string $open_mode = 'r+'): self

Creates a new object à la fopen.

<?php

use League\Csv\Reader;
use League\Csv\Writer;


$reader = Reader::createFromPath('/path/to/your/csv/file.csv');
//the $reader object will use the 'r+' open mode.
$writer = Writer::createFromPath('/path/to/your/csv/file.csv', 'w');

The $open_mode default to r+ if none is supplied.

Create from a resource stream

<?php

public static AbstractCsv::createFromStream(resource $stream): self

Creates a new object from a stream resource.

<?php

use League\Csv\Reader;
use League\Csv\Writer;

$reader = Reader::createFromStream(fopen('/path/to/the/file.csv', 'r+'));
$writer = Writer::createFromStream(tmpfile());

The resource stream MUST be seekable otherwise a InvalidArgumentException is thrown.

Create from a SPL file object

<?php

public static AbstractCsv::createFromFileObject(SplFileObject $obj): self

Creates a new object from a SplFileObject object.

<?php

use League\Csv\Reader;
use League\Csv\Writer;

$reader = Reader::createFromFileObject(new SplFileObject('/path/to/your/csv/file.csv'));
$writer = Writer::createFromFileObject(new SplTempFileObject());

The SplFileObject MUST be seekable otherwise a RuntimeException may be thrown.