The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records

Interoperability

Converting Records

Upgrading Guide

Document loading

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

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

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

Loading from a file path

<?php

public static AbstractCsv::createFromPath(
	string $path,
	string $open_mode = 'r+',
	resource $context = null
): 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', 'r');
$writer = Writer::createFromPath('/path/to/your/csv/file.csv', 'w');
Starting with version 9.1.0, $open_mode default to:
  • r+ for the Writer class
  • r for the Reader class

Loading 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());

Prior to version 9.1.0, the method was throwing League\Csv\Exception for non-seekable stream resource.

Loading from a SplFileObject 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());