Document loading

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

New API

Loading from a string

This new API is introduced in version 9.27.0

public static AbstractCsv::fromString(string $content = ''): self

Create a new object from a given string.

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

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

The $content argument default value is an empty string to ease usage.

Loading from a path

Since version 9.29.0
Since version 9.27.0 the createFromPath() method is deprecated
public static Reader::fromPath(SplFileInfo|string $path, string $mode = 'r', ?resource $context = null): Reader
public static Writer::fromPath(SplFileInfo|string $path, string $mode = 'r+', ?resource $context = null): Writer

Creates a new object à la fopen.

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

$reader = Reader::fromPath('/path/to/your/csv/file.csv', 'r');
$writer = Writer::fromPath(new SplFileInfo('/path/to/your/csv/file.csv'), 'w');

A SplFileObject does not expose its mode not its context. If it was created with one, you must pass explicitly its $mode and/or $context arguments. Alternatively, you can use the fromStream method.

Loading from stream

Since version 9.29.0
Since version 9.27.0 the createFromStream() and createFromFileObject() methods are deprecated
Starting with PHP8.6 Deprecation notices will be trigger if you use the package with SplFileObject instances. See Deprecation for PHP8.6
public static AbstractCsv::fromStream(SplFileObject|resource $stream): self

Creates a new object from a stream resource or a streaming object.

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

$reader = Reader::fromStream(fopen('/path/to/the/file.csv', 'r+'));
$writer = Writer::fromStream(tmpfile());
$reader = Reader::fromStream(new SplFileObject('/path/to/your/csv/file.csv'));
$writer = Writer::fromStream(new SplTempFileObject());

The provided stream—whether a resource or a SplFileObject—is used as-is. It is the developer’s responsibility to ensure that the stream is valid and has the appropriate permissions; otherwise, exceptions may be thrown during use.

Loading from a file pointer

This new API is introduced in version 9.27.0

public static AbstractCsv::from(
    SplFileInfo|SplFileObject|resource|string $filename,
    string $mode = 'r+',
    resource $context = null
): self

If a string or a SplFileInfo object is given as the $filename argument, a new instance is created à la fopen and the $mode and $context parameters are taken into account.

Otherwise, when a stream resource or an SplFileObject instance is given, both arguments are ignored.

Since version 9.27.0 this method can be use to replace the deprecated methods:
  • createFromPath()
  • createFromStream()
  • createFromFileObject()
use League\Csv\Reader;
use League\Csv\Writer;

$reader = Reader::from('/path/to/your/csv/file.csv', 'r');
$writer = Writer::from('/path/to/your/csv/file.csv', 'w');

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

$reader = Reader::from(new SplFileInfo('/path/to/your/csv/file.csv'));
$writer = Writer::from(new SplTempFileObject());
The $mode argument defaults to:
  • r+ for the Writer class
  • r for the Reader class

The method allows loading non-seekable stream resource.

Legacy API

The following methods are all deprecated as of version 9.27.0

Since version 9.1.0 non-seekable CSV documents can be used but exceptions will be thrown if features requiring a seekable CSV document are used.

Loading from a string

public static AbstractCsv::createFromString(string $content = ''): self

Creates a new object from a given string.

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');

Since version 9.2.0 the $content argument default value is an empty string to ease usage.

Loading from a file path

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

Creates a new object à la fopen.

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

$reader = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');
$writer = Writer::createFromPath('/path/to/your/csv/file.csv', 'w');
Starting with version 9.1.0, $open_mode defaults to:
  • r+ for the Writer class
  • r for the Reader class

Loading from a resource stream

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

Creates a new object from a stream resource.

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

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

Prior to version 9.1.0, the method would throw a League\Csv\Exception for a non-seekable stream resource.

Loading from a SplFileObject object

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

Creates a new object from a SplFileObject object.

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

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

Accessing the document path

New in version 9.2.0

public AbstractCsv::getPathname(): string

Once instantiated, the getPathname method returns the pathname of the underlying document.

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

Reader::from(new SplFileObject('/path/to/your/csv/file.csv'))->getPathname();
//returns '/path/to/your/csv/file.csv'
Writer::from(new SplTempFileObject())->getPathname();
//returns php://temp