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.

public AbstractCsv::toString(): string
public AbstractCsv::getContent(): string
public AbstractCsv::__toString(): string

The toString method is added in version 9.7.0 and replaces the getContent method which is deprecated.

The getContent method is added in version 9.1.0 and replaces the __toString method which is deprecated.

Use the toString method to return the full CSV content.

Example

use League\Csv\Reader;

$reader = Reader::from('/path/to/my/file.csv', 'r');
echo $reader->toString();

Exceptions and Errors

If the CSV document is not seekable a Exception or a RuntimeException may be thrown when using toString. A Fatal Error will be trigger when using the __toString method.

Using the toString method

use League\Csv\Writer;

$csv = Writer::from(new SplFileObject('php://output', 'w'));
$csv->insertOne(['foo', 'bar']);
echo $csv->toString();
//throws a RuntimeException because the SplFileObject is not seekable

Using the __toString or getContent methods

use League\Csv\Writer;

$csv = Writer::from(new SplFileObject('php://output', 'w'));
$csv->insertOne(['foo', 'bar']);
echo $csv;
echo $csv->getContent();
//throws a Fatal Error because no exception can be thrown by the __toString method

The __toString method is deprecated in version 9.1.0 and will be removed in the next major version.

Outputting the document into chunks

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 an OutOfRangeException will be thrown.

use League\Csv\Reader;

$reader = Reader::fromPath('/path/to/huge/file.csv', 'r');
foreach ($reader->chunk(1024) as $chunk) {
    echo $chunk;
}

Downloading the document

Starting with version 9.18.0, the download() method replaces the deprecated output() method.

To make your CSV document downloadable, use the download() method. It sends the document’s contents directly to the output buffer, allowing the browser to handle the response as a file download.

public AbstractCsv::download(?string $filename = null, int $chunk_size = 8192): int

The method returns the number of bytes read from the document and written to the output stream.

The download() method accepts two optional arguments:

  • $filename: An optional filename for the downloaded document. When a non-empty filename is provided, the appropriate HTTP headers are automatically generated. When null or an empty string is provided, no download headers are generated.
  • $chunk_size: The number of bytes read from the document in each iteration when streaming its contents. Defaults to 8192.

Default usage

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::from('/path/to/my/file.csv', 'r');
$reader->download();
die;

Using the $filename argument

use League\Csv\Reader;

$reader = Reader::from('file.csv');
$reader->download('name-for-your-file.csv');
die;

Since download() writes directly to the output buffer, terminate your script immediately after calling it to prevent additional output from being appended to the downloaded document. Do not return the method's return value as the response body.

Using the $chunk_size argument

use League\Csv\Reader;

$reader = Reader::from('file.csv');
$reader->download('name-for-your-file.csv', chunk_size: 1024);
die;

In this example, the document is streamed in chunks of 1024 bytes.

Using a Response object (Symfony, Laravel, PSR-7 etc)

To avoid breaking the flow of your application, you should create a Response object when applicable in your framework. The actual implementation will differ per framework, but you should generally not output headers directly.

use League\Csv\Reader;

$reader = Reader::from('/path/to/my/file.csv', 'r');
return new Response($reader->toString(), 200, [
    'Content-Encoding' => 'none',
    'Content-Type' => 'text/csv; charset=UTF-8',
    'Content-Disposition' => 'attachment; filename="name-for-your-file.csv"',
    'Content-Description' => 'File Transfer',
]);

In some cases you can also use a Streaming Response for larger files. The following example uses Symfony’s StreamedResponse object.

Be sure to adapt the following code to your own framework/situation. The following code is given as an example without warranty of it working out of the box.

use League\Csv\Writer;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;

//we generate the CSV using the Writer object
//$dbh is a PDO object
$stmt = $dbh->prepare("SELECT * FROM users");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$csv = Writer::from('php://temp', 'r+');
$csv->insertAll($stmt);

//we create a callable to output the CSV in chunks
//with Symfony StreamResponse you can flush the body content if necessary
//see Symfony documentation for more information
$flush_threshold = 1000; //the flush value should depend on your CSV size.
$content_callback = function () use ($csv, $flush_threshold) {
    foreach ($csv->chunk(1024) as $offset => $chunk) {
        echo $chunk;
        if ($offset % $flush_threshold === 0) {
            flush();
        }
    }
};

//we send the CSV using Symfony StreamedResponse
$response = new StreamedResponse();
$response->headers->set('Content-Encoding', 'none');
$response->headers->set('Content-Type', 'text/csv; charset=UTF-8');

$disposition = $response->headers->makeDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
    'name-for-your-file.csv'
);

$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Description', 'File Transfer');
$response->setCallback($content_callback);
$response->send();

Notes

The output methods can only be affected by:

No other method or property has an effect on them.