The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records

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 methods output are affected by the output BOM sequence 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 show 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();

Using the $filename argument

<?php

use League\Csv\Reader;

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

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";

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