PHPackages                             lukaswerner/php-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. lukaswerner/php-csv

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

lukaswerner/php-csv
===================

A simple csv processing library for php

0.2.3(7y ago)024.1k↓30.4%GPL-3.0-or-laterPHPPHP &gt;=7.1.0

Since Jan 13Pushed 7y ago1 watchersCompare

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

READMEChangelog (5)Dependencies (1)Versions (6)Used By (0)

php-csv
=======

[](#php-csv)

This library focuses on simplicity and memory efficiency when processing CSV files in PHP.

Features
--------

[](#features)

- Simple API
- Read CSV files line by line
- Memory efficiency: File is read line by line, no memory issues with large files
- Process CSV using row and chunk callbacks
- Fully documented
- Fully unit tested
- Composer ready

System Requirements
-------------------

[](#system-requirements)

You need **PHP** &gt;= **7.1.0** to use `php-csv` but the latest stable version of PHP is recommended.

Install
-------

[](#install)

Install `php-csv` using Composer.

```
$ composer require lukaswerner/php-csv

```

Use
---

[](#use)

You can initialize an object of the main `PhpCsv\Csv` class by providing a valid `\SplFileInfo` object to the constructor:

```
$fileInfo = new \SplFileInfo('/path/to/file.csv');
$csv = new \PhpCsv\Csv($fileInfo);

```

You now have an easy to understand API to process your CSV file. Each processing method will at first rewind the file, so that you don't have any issues with file pointers. If your file does not contain a header row, you must call `$csv->setContainsHeader(false);`.

### Number of lines

[](#number-of-lines)

You can easily count the number of lines in your csv file by calling `$csv->count();`. This method will only once examine all lines. On second call it will use its previously set value. To avoid that, you have to create a new object of `\PhpCsv\Csv`.

#### Generator return values

[](#generator-return-values)

The generator, created with `$csv->rows()` call will now have a return value if not aborted. That means, after iterating through **all** lines in your CSV file, you can get the line count as the generator return. Do something like this:

```
$rowsGenerator = $csv->rows();
foreach ($rowsGenerator as $row) {
    // Do something
}

$numberOfLines = (int)$rowsGenerator->getReturn();

```

Please note, that this will throw an exception, if you use break in your loop. Just like that, the `$csv->process()` will now return that generator return value or null, if aborted.

### Reading header

[](#reading-header)

You can read the header fields by calling `$csv->header();`. This will return an array of strings with header information. This method will only once examine all lines. On second call it will use its previously set value. To avoid that, you have to create a new object of `\PhpCsv\Csv`.

### Iterating over rows

[](#iterating-over-rows)

You can iterate over all rows of your csv file by doing something like this:

```
foreach ($csv->rows() as $lineNumber => $row) {
    // Do somethine with row
}

```

The `$lineNumber` contains the current line number, starting with `1`. The header (if existing) is not included. So a file with a header and one line has `$lineCount = 1` on that line.

The `rows()` method returns a PHP Generator and loops can be continued or stopped as you wish.

### Processing using callbacks

[](#processing-using-callbacks)

The `php-csv` library offers a comfortable `process()` method to process the CSV file using callbacks. You can do something like this:

```
$csv->process(
    function (array $row, int $lineNumber): bool {
        // Do something with your data

        return true; // If you return false here, the processing will stop
    }
);

```

#### Chunking

[](#chunking)

The callback processing also allows chunking. That means, you have a callback for each row and additionally a callback for each chunk. At first you can set the chunk size with `$csv->setChunkSize(500)` (default is 1000), then you call the `process()` method like this:

```
$csv->process(
    function (array $row, int $lineNumber): bool {
        // Do something with your data

        return true; // If you return false here, the processing will stop
    },
    function (): void {
        // Will be called e.g. every 500th row
        // you can flush your values to db or something like that
    }
);

```

Please note, that the chunk callback will be called one last time, when you return `false` from the row callback. That is to be able to flush incomplete chunks.

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

[](#contributing)

Contributions are welcome and will be fully credited. Please feel free to open issues and ask for repository access.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

License
-------

[](#license)

The GNU General Public License v3.0. Please see [LICENSE](LICENSE) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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 ~0 days

Total

5

Last Release

2681d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/03be389b62ec641c98e4ffde441a45c6f13f030f00d8d1f3bd03bfab2156dd13?d=identicon)[lukaswerner](/maintainers/lukaswerner)

---

Top Contributors

[![blablablubb89](https://avatars.githubusercontent.com/u/44839731?v=4)](https://github.com/blablablubb89 "blablablubb89 (4 commits)")[![finkformatics](https://avatars.githubusercontent.com/u/6151232?v=4)](https://github.com/finkformatics "finkformatics (1 commits)")[![lukaswerner](https://avatars.githubusercontent.com/u/116719312?v=4)](https://github.com/lukaswerner "lukaswerner (1 commits)")

---

Tags

processingcsvimportreadwritechunk

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[league/csv

CSV data manipulation made easy in PHP

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

PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way

1.2k57.6M131](/packages/openspout-openspout)[shuchkin/simplecsv

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

5192.4k](/packages/shuchkin-simplecsv)

PHPackages © 2026

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