PHPackages                             luchaninov/csv-file-loader - 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. luchaninov/csv-file-loader

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

luchaninov/csv-file-loader
==========================

Load CSV &amp; TSV files and strings. Using generators to minimize memory usage

1.10.0(11mo ago)1134.9k↓12.5%2MITPHPPHP &gt;=8.1

Since Oct 31Pushed 11mo ago3 watchersCompare

[ Source](https://github.com/luchaninov/csv-file-loader)[ Packagist](https://packagist.org/packages/luchaninov/csv-file-loader)[ RSS](/packages/luchaninov-csv-file-loader/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (18)Used By (0)

CSV File Loader
===============

[](#csv-file-loader)

Load CSV &amp; TSV files and strings using PHP generators. It uses memory like `fopen` but requires less code.

How to Install
--------------

[](#how-to-install)

Install the `luchaninov/csv-file-loader` package using [composer](http://getcomposer.org/):

```
$ composer require luchaninov/csv-file-loader
```

Basic Usage
-----------

[](#basic-usage)

```
$loader = new CsvFileLoader('/path/to/your_data.csv');

foreach ($loader->getItems() as $item) {
    var_dump($item); // do something here
}
```

If you have CSV-file

```
id,name,surname
1,Jack,Black
2,John,Doe

```

you'll get 2 items

```
['id' => '1', 'name' => 'Jack', 'surname' => 'Black']
['id' => '2', 'name' => 'John', 'surname' => 'Doe']

```

It uses [fgetcsv](http://php.net/fgetcsv) function so it understands enclosed values like

```
item1,"item2,still item2",item3

```

and even

```
item1,"item2
still item2",item3

```

If you have simple string, no need to save it to the file. Use

```
(new CsvStringLoader($s))->getItemsArray();
(new TsvStringLoader($s))->getItemsArray();
(new AutoStringLoader($s))->getItemsArray();
```

Advanced Usage
--------------

[](#advanced-usage)

If file is not large you can load all items at once without generators using `getItemsArray()`.

If you have custom delimiters use `setDelimiter` like `$loader->setDelimiter(';')`. Same with encloser - `setEncloser`. Default delimiter is `,` for `CsvFileLoader` and `\t` for `TsvFileLoader`; default encloser is `"`. If you are not sure - use delimiter `auto`, it detects delimiter automatically by the first 10,000 characters.

If you have TSV instead of CSV you can set use `setDelimiter("\t")` or use `TsvFileLoader`. If you are not sure - use `AutoFileLoader`, it detects delimiter automatically.

By default it assumes that the first row of the file contains headers - it doesn't return this row as item but uses as keys for next rows. If you don't have headers in the first row - you can:

- set your own keys - `setHeaders(['key1', 'key2', ...])`
- use numerical keys `[0, 1, 2, ...]` - `setHeaders(false)`

If there are more cols in some rows than there are cols in headers then they are truncated. If you prefer to add extra values with numerical keys use `setAddUnknownColumns(true)`.

To count items use `countItems()`. In case of CSV it's not always the same with rows count - `wc -l`, because one item can have several rows.

You can use same loader to load several files - `$loader->setFilename('other_file.csv')`. If you iterate during some file when calling `setFilename` then there will be no more items from the first file, foreach will just finish.

Code is very simple - look at sources and tests.

TxtFileLoader
-------------

[](#txtfileloader)

If you have simple text file use `TxtFileLoader`.

It makes from file

```
text1
text2
text3

```

array `['text1', 'text2', 'text3']`.

### setSkipEmptyRows

[](#setskipemptyrows)

Skips empty rows or containing only whitespaces `trim($s) === ''`. Default: `true`.

### setSkipComments

[](#setskipcomments)

Skips rows that start with `#` or `\s+#`. Default: `false`.

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance52

Moderate activity, may be stable

Popularity36

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% 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 ~220 days

Recently: every ~256 days

Total

17

Last Release

335d ago

PHP version history (2 changes)1.0.1PHP &gt;=5.5.0

1.8.0PHP &gt;=8.1

### Community

Maintainers

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

---

Top Contributors

[![luchaninov](https://avatars.githubusercontent.com/u/3829796?v=4)](https://github.com/luchaninov "luchaninov (44 commits)")

---

Tags

generatorloadercsvtsv

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/luchaninov-csv-file-loader/health.svg)

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

###  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)[rap2hpoutre/fast-excel

Fast Excel import/export for Laravel

2.3k24.9M47](/packages/rap2hpoutre-fast-excel)[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)[faisalman/simple-excel-php

Easily parse / convert / write between Microsoft Excel XML / CSV / TSV / HTML / JSON / etc formats

582599.4k1](/packages/faisalman-simple-excel-php)[initred/laravel-tabula

laravel-tabula is a tool for liberating data tables trapped inside PDF files for the Laravel framework.

1418.6k](/packages/initred-laravel-tabula)

PHPackages © 2026

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