PHPackages                             goodby/csv - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. goodby/csv

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

goodby/csv
==========

CSV import/export library

1.3.0(10y ago)9555.6M↓10.1%148[27 issues](https://github.com/goodby/csv/issues)[11 PRs](https://github.com/goodby/csv/pulls)20MITPHPPHP &gt;=5.3.2CI failing

Since Dec 6Pushed 2y ago31 watchersCompare

[ Source](https://github.com/goodby/csv)[ Packagist](https://packagist.org/packages/goodby/csv)[ Docs](https://github.com/goodby/csv)[ RSS](/packages/goodby-csv/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (20)

Goodby, CSV
===========

[](#goodby-csv)

[![Build Status](https://camo.githubusercontent.com/26771bfe59f268c6693b7e7ed11d4590dba658dbf1f45ec39dfc2e287a371c3a/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f676f6f6462792f6373762e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/goodby/csv)

What is "Goodby CSV"?
---------------------

[](#what-is-goodby-csv)

Goodby CSV is a highly memory efficient, flexible and extendable open-source CSV import/export library.

```
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

$lexer = new Lexer(new LexerConfig());
$interpreter = new Interpreter();
$interpreter->addObserver(function(array $row) {
    // do something here.
	// for example, insert $row to database.
});
$lexer->parse('data.csv', $interpreter);
```

### Features

[](#features)

#### 1. Memory Management Free

[](#1-memory-management-free)

This library was designed for low memory usage. It will not accumulate all the rows in the memory. The importer reads a CSV file and executes a callback function line by line.

#### 2. Multibyte support

[](#2-multibyte-support)

This library supports mulitbyte input/output: for example, SJIS-win, EUC-JP and UTF-8.

#### 3. Ready to Use for Enterprise Applications

[](#3-ready-to-use-for-enterprise-applications)

Goodby CSV is fully unit-tested. The library is stable and ready to be used in large projects like enterprise applications.

Requirements
------------

[](#requirements)

- PHP 5.3.2 or later
- mbstring

Installation
------------

[](#installation)

Install composer in your project:

```
curl -s http://getcomposer.org/installer | php
```

Create a `composer.json` file in your project root:

```
{
    "require": {
        "goodby/csv": "*"
    }
}
```

Install via composer:

```
php composer.phar install
```

Documentation
-------------

[](#documentation)

### Configuration

[](#configuration)

Import configuration:

```
use Goodby\CSV\Import\Standard\LexerConfig;

$config = new LexerConfig();
$config
    ->setDelimiter("\t") // Customize delimiter. Default value is comma(,)
    ->setEnclosure("'")  // Customize enclosure. Default value is double quotation(")
    ->setEscape("\\")    // Customize escape character. Default value is backslash(\)
    ->setToCharset('UTF-8') // Customize target encoding. Default value is null, no converting.
    ->setFromCharset('SJIS-win') // Customize CSV file encoding. Default value is null.
;
```

Export configuration:

```
use Goodby\CSV\Export\Standard\ExporterConfig;

$config = new ExporterConfig();
$config
    ->setDelimiter("\t") // Customize delimiter. Default value is comma(,)
    ->setEnclosure("'")  // Customize enclosure. Default value is double quotation(")
    ->setEscape("\\")    // Customize escape character. Default value is backslash(\)
    ->setToCharset('SJIS-win') // Customize file encoding. Default value is null, no converting.
    ->setFromCharset('UTF-8') // Customize source encoding. Default value is null.
    ->setFileMode(CsvFileObject::FILE_MODE_WRITE) // Customize file mode and choose either write or append. Default value is write ('w'). See fopen() php docs
;
```

### Unstrict Row Consistency Mode

[](#unstrict-row-consistency-mode)

By default, Goodby CSV throws `StrictViolationException` when it finds a row with a different column count to other columns. In the case you want to import such a CSV, you can call `Interpreter::unstrict()` to disable row consistency check at import.

rough.csv:

```
foo,bar,baz
foo,bar
foo
foo,bar,baz
```

```
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\LexerConfig;

$interpreter = new Interpreter();
$interpreter->unstrict(); // Ignore row column count consistency

$lexer = new Lexer(new LexerConfig());
$lexer->parse('rough.csv', $interpreter);
```

Examples
--------

[](#examples)

### Import to Database via PDO

[](#import-to-database-via-pdo)

user.csv:

```
1,alice,alice@example.com
2,bob,bob@example.com
3,carol,carol@eample.com
```

```
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');

$config = new LexerConfig();
$lexer = new Lexer($config);

$interpreter = new Interpreter();

$interpreter->addObserver(function(array $columns) use ($pdo) {
    $stmt = $pdo->prepare('INSERT INTO user (id, name, email) VALUES (?, ?, ?)');
    $stmt->execute($columns);
});

$lexer->parse('user.csv', $interpreter);
```

### Import from TSV (tab separated values) to array

[](#import-from-tsv-tab-separated-values-to-array)

temperature.tsv:

```
9	Tokyo
27	Singapore
-5	Seoul
7	Shanghai
```

```
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

$temperature = array();

$config = new LexerConfig();
$config->setDelimiter("\t");
$lexer = new Lexer($config);

$interpreter = new Interpreter();
$interpreter->addObserver(function(array $row) use (&$temperature) {
    $temperature[] = array(
        'temperature' => $row[0],
        'city'        => $row[1],
    );
});

$lexer->parse('temperature.tsv', $interpreter);

print_r($temperature);
```

### Export from array

[](#export-from-array)

```
use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;

$config = new ExporterConfig();
$exporter = new Exporter($config);

$exporter->export('php://output', array(
    array('1', 'alice', 'alice@example.com'),
    array('2', 'bob', 'bob@example.com'),
    array('3', 'carol', 'carol@example.com'),
));
```

### Export from database via PDO

[](#export-from-database-via-pdo)

```
use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;
use Goodby\CSV\Export\Standard\CsvFileObject;
use Goodby\CSV\Export\Standard\Collection\PdoCollection;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');

$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');
$pdo->query("INSERT INTO user VALUES(1, 'alice', 'alice@example.com')");
$pdo->query("INSERT INTO user VALUES(2, 'bob', 'bob@example.com')");
$pdo->query("INSERT INTO user VALUES(3, 'carol', 'carol@example.com')");

$config = new ExporterConfig();
$exporter = new Exporter($config);

$stmt = $pdo->prepare("SELECT * FROM user");
$stmt->execute();

$exporter->export('php://output', new PdoCollection($stmt));
```

### Export with CallbackCollection

[](#export-with-callbackcollection)

```
use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;

use Goodby\CSV\Export\Standard\Collection\CallbackCollection;

$data = array();
$data[] = array('user', 'name1');
$data[] = array('user', 'name2');
$data[] = array('user', 'name3');

$collection = new CallbackCollection($data, function($row) {
    // apply custom format to the row
    $row[1] = $row[1] . '!';

    return $row;
});

$config = new ExporterConfig();
$exporter = new Exporter($config);

$exporter->export('php://stdout', $collection);
```

### Export in Symfony2 action

[](#export-in-symfony2-action)

```
namespace AcmeBundle\ExampleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\StreamedResponse;

class DefaultController extends Controller
{
	public function csvExportAction()
	{
		$conn = $this->get('database_connection');

		$stmt = $conn->prepare('SELECT * FROM somewhere');
		$stmt->execute();

		$response = new StreamedResponse();
		$response->setStatusCode(200);
		$response->headers->set('Content-Type', 'text/csv');
		$response->setCallback(function() use($stmt) {
			$config = new ExporterConfig();
			$exporter = new Exporter($config);

		    $exporter->export('php://output', new PdoCollection($stmt->getIterator()));
		});
		$response->send();

		return $response;
	}
}
```

License
-------

[](#license)

Csv is open-sourced software licensed under the MIT License - see the LICENSE file for details

Contributing
------------

[](#contributing)

We works under test driven development.

Checkout master source code from github:

```
hub clone goodby/csv
```

Install components via composer:

```
# If you don't have composer.phar
./scripts/bundle-devtools.sh .

# If you have composer.phar
composer.phar install --dev

```

Run phpunit:

```
./vendor/bin/phpunit
```

Acknowledgement
---------------

[](#acknowledgement)

Credits are found within composer.json file.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity67

Solid adoption and visibility

Community43

Growing community involvement

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~233 days

Total

5

Last Release

3976d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/77f381b8b8abb6451a7ee2e4f6a4f5d69427193680024479b5ebb0b646cf9e90?d=identicon)[reoring](/maintainers/reoring)

![](https://www.gravatar.com/avatar/df0e4439766a9bc915e2db0e797f10f222b701133dd0c20fc2ddcc658078eb9f?d=identicon)[suin](/maintainers/suin)

---

Top Contributors

[![suin](https://avatars.githubusercontent.com/u/855338?v=4)](https://github.com/suin "suin (68 commits)")[![reoring](https://avatars.githubusercontent.com/u/735890?v=4)](https://github.com/reoring "reoring (10 commits)")[![alexander-schranz](https://avatars.githubusercontent.com/u/1698337?v=4)](https://github.com/alexander-schranz "alexander-schranz (5 commits)")[![zackkatz](https://avatars.githubusercontent.com/u/870979?v=4)](https://github.com/zackkatz "zackkatz (2 commits)")[![Richtermeister](https://avatars.githubusercontent.com/u/624921?v=4)](https://github.com/Richtermeister "Richtermeister (2 commits)")[![sasezaki](https://avatars.githubusercontent.com/u/42755?v=4)](https://github.com/sasezaki "sasezaki (2 commits)")[![jared-fraser](https://avatars.githubusercontent.com/u/1113900?v=4)](https://github.com/jared-fraser "jared-fraser (1 commits)")[![ldimlight](https://avatars.githubusercontent.com/u/10096269?v=4)](https://github.com/ldimlight "ldimlight (1 commits)")[![maclof](https://avatars.githubusercontent.com/u/4358583?v=4)](https://github.com/maclof "maclof (1 commits)")[![meonkeys](https://avatars.githubusercontent.com/u/50639?v=4)](https://github.com/meonkeys "meonkeys (1 commits)")[![nagodon](https://avatars.githubusercontent.com/u/604596?v=4)](https://github.com/nagodon "nagodon (1 commits)")[![obalais](https://avatars.githubusercontent.com/u/807362?v=4)](https://github.com/obalais "obalais (1 commits)")[![pborreli](https://avatars.githubusercontent.com/u/77759?v=4)](https://github.com/pborreli "pborreli (1 commits)")[![rene-roesch-blg](https://avatars.githubusercontent.com/u/33286651?v=4)](https://github.com/rene-roesch-blg "rene-roesch-blg (1 commits)")[![gsmark](https://avatars.githubusercontent.com/u/5028416?v=4)](https://github.com/gsmark "gsmark (1 commits)")[![emmanuelgautier](https://avatars.githubusercontent.com/u/2765366?v=4)](https://github.com/emmanuelgautier "emmanuelgautier (1 commits)")[![cordoval](https://avatars.githubusercontent.com/u/328359?v=4)](https://github.com/cordoval "cordoval (1 commits)")[![hanneskaeufler](https://avatars.githubusercontent.com/u/68024?v=4)](https://github.com/hanneskaeufler "hanneskaeufler (1 commits)")[![iplesca](https://avatars.githubusercontent.com/u/5592031?v=4)](https://github.com/iplesca "iplesca (1 commits)")

---

Tags

exportcsvimport

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/goodby-csv/health.svg)

```
[![Health](https://phpackages.com/badges/goodby-csv/health.svg)](https://phpackages.com/packages/goodby-csv)
```

###  Alternatives

[maatwebsite/excel

Supercharged Excel exports and imports in Laravel

12.7k144.3M712](/packages/maatwebsite-excel)[league/csv

CSV data manipulation made easy in PHP

3.5k166.1M646](/packages/league-csv)[handcraftedinthealps/goodby-csv

CSV import/export library

441.6M4](/packages/handcraftedinthealps-goodby-csv)[ufirst/lang-import-export

A Laravel package providing artisan commands to import and export language files from and to CSV.

42207.9k](/packages/ufirst-lang-import-export)[shuchkin/simplecsv

Parse and retrieve data from CSV files. Export data to CSV.

5192.4k](/packages/shuchkin-simplecsv)[highsolutions/laravel-lang-import-export

A Laravel package providing artisan commands to import and export language files from and to CSV.

25292.3k](/packages/highsolutions-laravel-lang-import-export)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
