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

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

idct/php-csv-writer
===================

CsvWriter based on a buffered TextWriter with EOL support.

1.0.5(6y ago)02.0k1MITPHPPHP &gt;=7.1CI failing

Since Nov 14Pushed 6y ago1 watchersCompare

[ Source](https://github.com/bpacholek/php-csv-writer)[ Packagist](https://packagist.org/packages/idct/php-csv-writer)[ RSS](/packages/idct-php-csv-writer/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (5)Versions (8)Used By (1)

PHP CSV Writer
==============

[](#php-csv-writer)

Basic CsvWriter based on a buffered TextWriter (also provided). Supports buffering in memory and proper handling of EOL setting which is not available easily with native `fputcsv` function.

[![Tests status](https://github.com/ideaconnect/php-csv-writer/workflows/All%20tests%20using%20PHPUnit/badge.svg)](https://github.com/ideaconnect/php-csv-writer/workflows/All%20tests%20using%20PHPUnit/badge.svg) [![Coverage Status](https://camo.githubusercontent.com/5667887d1b8997305b55f59f2e7a1d078da99c79ef43de00b16bbba941e585b2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f69646561636f6e6e6563742f7068702d6373762d7772697465722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/ideaconnect/php-csv-writer?branch=master) [![GitHub tag (latest SemVer)](https://camo.githubusercontent.com/0208613c6ced1fabee2bc22c8a9e13c6eb526b2a5b4216cab5fcd84dcd826e0b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f69646561636f6e6e6563742f7068702d6373762d7772697465723f6c6162656c3d6c617465737425323076657273696f6e26736f72743d73656d766572)](https://camo.githubusercontent.com/0208613c6ced1fabee2bc22c8a9e13c6eb526b2a5b4216cab5fcd84dcd826e0b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f69646561636f6e6e6563742f7068702d6373762d7772697465723f6c6162656c3d6c617465737425323076657273696f6e26736f72743d73656d766572)

Main purpose of this library is to provide an object-oriented way of buffered CSV files writing.

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

[](#installation)

The best way to install the library in your project is by using **Composer**:

```
composer require idct/php-csv-writer
```

of course you can still manually include all the required files in your project using `using` statements yet **Composer** and autoloading is more than suggested.

Usage
-----

[](#usage)

Create an instance:

```
use IDCT\CsvWriter\CsvWriter;
$csvWriter = new CsvWriter();
```

Now you need to open or create a file, to do so you have two methods available: **open** and **openWithFieldsNames**To create a new file with without adding headers in the first line use:

```
$csvWriter->open('filename.csv');
```

To open a new file with fields' names added in the first line pass an array as the second argument:

```
$csvWriter->openWithFieldsNames('filename.csv', ["columnA", "columnB", "columnC"]);
```

**When writing a new line (adding data) to a CSV file opened with fields names then columns count number is verified on each attempt.**

It is possible to appened into existing files by passing **TextWriter::FILEMODE\_APPEND** as the last argument.

```
$csvWriter->open('filename.csv', CsvWriter::FILEMODE_APPEND);
```

or:

```
$csvWriter->openWithFieldsNames('filename.csv', ["columnA", "columnB", "columnC"], CsvWriter::FILEMODE_APPEND);
```

**Warning: in append mode lines are NOT added as the first line of the CSV file, they are used only for verification of columns count during write operations.**

### Buffering

[](#buffering)

By default **CsvWriter** has buffering disabled, you can enable it by providing buffer size (in bytes) using method **setBufferSize**. Be sure to understand that data is actually saved in the file only when buffer is filled so be sure to close the file using **close** method or use **flush** whenever you want to intentionally flush the buffer into the file without closing it.

### EOL support

[](#eol-support)

By default PHP's internal `fputcsv` function uses default EOL symbol for the current platform. `idct/php-csv-writer` supports setting different EOL symbols: pass `CsvWriter::EOL_WINDOWS`, `CsvWriter::EOL_LINUX` or `CsvWriter::EOL_MACLEGACY` (Mac today uses LINUX EOL, this is just for legacy support) to **setEolSymbol** method. **Warning:** the EOL symbol will be used only for **NEXT** lines, previous ones (already existing in the file or buffer) will remain intact.

### TextWriter

[](#textwriter)

CsvWriter is based on **TextWriter** which can be used for buffered text write operations. Apart from the methods described above it provides also different behavior for `write` and `writeln` operations which store the text or write it followed by the defined EOL symbol respectively. To use it just create an instance:

```
$textWriter = new TextWriter();
```

TODO / Contribution
-------------------

[](#todo--contribution)

At the moment the main requirement is to provide better unit tests and documentation, yet if you find any bugs or have potential feature ideas then please use Issues or Pull Requests, it is more than welcome! I will try to reply ASAP.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

Total

7

Last Release

2345d ago

Major Versions

0.1 → 1.0.02019-11-17

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/168780730?v=4)[IDCT Bartosz Pachołek](/maintainers/ideaconnect)[@ideaconnect](https://github.com/ideaconnect)

---

Tags

csvcsv-convertercsv-exportcsv-filescsv-formatcsv-parsercsv-parsingcsvwritercsviotextidcteoltextwritercsvwriter

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[maatwebsite/excel

Supercharged Excel exports and imports in Laravel

12.7k152.8M847](/packages/maatwebsite-excel)[league/csv

CSV data manipulation made easy in PHP

3.5k176.5M795](/packages/league-csv)[smalot/pdfparser

Pdf parser library. Can read and extract information from pdf file.

2.7k38.3M255](/packages/smalot-pdfparser)[rap2hpoutre/fast-excel

Fast Excel import/export for Laravel

2.3k26.3M50](/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.2k65.5M209](/packages/openspout-openspout)[kartik-v/yii2-export

A library to export server/db data in various formats (e.g. excel, html, pdf, csv etc.)

1613.2M36](/packages/kartik-v-yii2-export)

PHPackages © 2026

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