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

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

CSV document output

Once your CSV document is loaded, you can print or enable downloading it using the methods below.

The output methods are affected by the output BOM sequence and/or the supplied PHP stream filters.

Even though you can use the following methods with the League\Csv\Writer object. It is recommended to do so with the League\Csv\Reader class to avoid losing the file cursor position and getting unexpected results when inserting new data.

Printing the document

Returns the string representation of the CSV document

<?php

public AbstractCsv::__toString(void): string

Use the echo construct on the instantiated object or use the __toString method to return the CSV full content.

Example

<?php

use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv');
echo $reader;
// or
echo $reader->__toString();

Downloading the document

To make your CSV document downloadable use the output method to force the use of the output buffer on the CSV content.

<?php

public AbstractCsv::output(string $filename = null): int

The method returns the number of characters read from the handle and passed through to the output.

The output method can take an optional argument $filename. When present you can even remove more headers.

Default usage

<?php

use League\Csv\Reader;

header('Content-Type: text/csv; charset=UTF-8');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="name-for-your-file.csv"');

$reader = Reader::createFromPath('/path/to/my/file.csv');
$reader->output();
die;

Using the $filename argument

<?php

use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv');
$reader->output("name-for-your-file.csv");
die;

If you just need to make the CSV downloadable, end your script with a call to exit just after the output method. You should not return the method returned value.

Outputting the document into chunks

<?php

public AbstractCsv::chunk(int $length): Generator

The AbstractCsv::chunk method takes a single $length parameter specifying the number of bytes to read from the CSV document and returns a Generator to ease outputting large CSV files.

if the $length parameter is not a positive integer a OutOfRangeException will be thrown.

<?php

use League\Csv\Reader;

header('Transfer-Encoding: chunked');
header('Content-Encoding: none');
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="name-for-your-file.csv"');

$reader = Reader::createFromPath('/path/to/huge/file.csv', 'r');
foreach ($reader->chunk(1024) as $chunk) {
    echo dechex(strlen($chunk))."\r\n$chunk\r\n";
}
echo "0\r\n\r\n";

If your application is using a framework, to avoid breaking its flow, you should create a framework specific Response object when applicable using AbstractCsv::__toString or AbstractCsv::chunk depending on the size of your CSV document.