Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Csv Laravel Package

league/csv

View on GitHub
Deep Wiki
Context7

layout: default title: Basic Usage redirect_from: /basic-usage/

Basic usage

Once your CSV object is instantiated and configured, you can start interacting with the data using a number of methods available to you.

Iterating over the CSV rows

The CSV object implements PHP's IteratorAggregate interface

public AbstractCsv::getIterator(void): Iterator

You can iterate over your CSV object to extract each CSV row using the foreach construct.

use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
foreach ($reader as $index => $row) {
    //do something meaningful here with $row !!
    //$row is an array where each item represent a CSV data cell
    //$index is the CSV row index
}

Outputting the CSV

__toString

Returns the string representation of the CSV document

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

use League\Csv\Reader;

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

output

If you only wish to make your CSV downloadable by forcing a file download just use the output method to force the use of the output buffer on the CSV content.

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.

Example 1 - default usage

use League\Csv\Reader;

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

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

Example 2 - using the $filename argument

use League\Csv\Reader;

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

Example 3 - 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. In some cases you can also use a Streaming Response for larger files.

use League\Csv\Reader;

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

Notes

The output methods can only be affected by:

No other method or property have effect on them.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport