PHPackages                             kanryu/quick-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. kanryu/quick-csv

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

kanryu/quick-csv
================

Portable PHP library that allows you to import and export CSV very fast by issuing special queries to RDBs.

v1.0.3(6y ago)18MITPHPPHP &gt;=5.3CI failing

Since Oct 28Pushed 6y ago1 watchersCompare

[ Source](https://github.com/kanryu/quick-csv)[ Packagist](https://packagist.org/packages/kanryu/quick-csv)[ Docs](https://github.com/kanryu/quick-csv)[ RSS](/packages/kanryu-quick-csv/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (5)Used By (0)

QuickCsv
========

[](#quickcsv)

Portable PHP library that allows you to import and export CSV very fast by executing special queries to RDBs.

Why is QuickCsv so fast?
------------------------

[](#why-is-quickcsv-so-fast)

Compared with the input value from the web form, the data imported by CSV has many rows.

Each row in the CSV has a number of columns, and you will need to check for different validations. The row may already exist in the database or may not exist yet. You will check each line and the final data will be input by either INSERT/UPDATE. Since there are a lot of rows, you need to write and execute these operations for the number of rows x columns. It has slow results despite being very hard to implement. You will be disappointed.

QuickCsv solves the CSV import process flexibly and quickly. You just give the column information that the CSV data has as an Array, and the library automatically creates the table and put all the rows of the CSV on it. Without implementing the validation process in PHP, the values of all columns of all rows, correlation check between rows, foreign key constraints with foreign tables, etc. can be solved quickly with SQL. All of these are automatically generated and executed by QuickCsv.

Your service with QuickCsv
--------------------------

[](#your-service-with-quickcsv)

By adopting this library, you will be able to provide such services to users.

- Resolving different character codes between CSV data and database
- Accept large amounts of CSV very fast
- Accept any CSV column added, omitted, rearranged (but you need to understand the column order before processing)
- Accept user-defined columns that do not exist as columns in the destination table

Install
-------

[](#install)

```
composer require kanryu/quick-csv
```

Usage
-----

[](#usage)

```
use Kanryu\QuickCsv\QuickCsvImporter;

// -- (initialize begins)
$table_field_tmpl = array(
    array('name' => 'productId',   'type' => 'decimal(15)',   'maxlength' => 15,   ),
    array('name' => 'categoryId',  'type' => 'decimal(9)',    'maxlength' => 9,    'required' => true),
    array('name' => 'productCode', 'type' => 'varchar',       'maxlength' => 20,   ),
    array('name' => 'productName', 'type' => 'varchar',       'maxlength' => 40,   'required' => true),
    array('name' => 'price',       'type' => 'decimal(8,2)',  'maxlength' => 8,    'required' => true),
    array('name' => 'cost',        'type' => 'decimal(14,5)', 'maxlength' => 14,   'default' => "NULL"),
    array('name' => 'deleteFlag',  'type' => 'decimal(1)',    'maxlength' => 1,    'default' => "'0'", 'custom' => "deleteFlag BETWEEN '0' AND '1'"),
);
$qcsv = new QuickCsvImporter([
    'destTableName' => 'Product',
    'destPrimaryKey' => 'productId',
    'fieldSchema' => $table_field_tmpl
]);
$qcsv->setPdo($pdo); // set your pdo or other RDB drivers;

$qcsv->create(); // create temporary table for importing
$qcsv->import('./test.csv'); // import csv to the temporary table
// -- (initialize ends)

// -- (validate begins)
$qcsv->validateAllFields(); // validate all records, all fields
$qcsv->validateDuplicatedId('productId'); // validate duplicated uniqued field

// If the specified field is the primary key of an external table, the key must exist in the external table.
$qcsv->validateNonExistForeignKey('categoryId', 'categoryId', 'Category', 'deleteFlag = 0');
// -- (validate ends)

// -- merge to the destination table
$qcsv->updateExistingRecords(); // Overwrite records existing in the destination table with CSV
$qcsv->insertNonExistingRecords(); // Add a new record from CSV that does not exist in the destination table
```

CSV Field Schema
----------------

[](#csv-field-schema)

You give QuickCsvImporter an CSV column definition as an array. This is actually created as a temporary table. Imported CSV data is validated.

- **name**
    - The name of the CSV column. It becomes the field name of the temporary table.
    - If the whole is given as an associative array, the name property of each column can be given as a key of the outer array. In that case, it can be omitted.
- **type**
    - The data type that the field assumes. The temporary table is once imported as VARCHAR and it is determined whether it can be CAST() to the given type.
    - Available: *varchar, alphanumeric, datetime, date, decimal(n), decimal(n,m)*
        - Note: *int, tinyint* fields as *decimal(n)*
    - If varchar, do nothing. For other types, some confirmation is made.
    - 'alphanumeric' is `REGEXP '^[a-zA-Z0-9\-]+$'`. e.g. `'abcde123'`, `'123-456'`
    - You can add a new type by calling setValidatorForType().
    - Errors: *XXX\_notalphanumeric, XXX\_notdatetime, XXX\_notdecimal*
- **maxlength**
    - Determine the length of the string entered in the field.
    - Each field of the temporary table is defined as a column of maxlength + 1 characters.
    - Errors: *XXX\_maxlength*
- **field** (optional)
    - Define the field schema of the temporary table by manual.
    - However, since the DEFAULT option of 'CREATE TABLE' has no effect during CSV import, specify the **default** key.
    - Without **field** key, auto generated as `{$name} VARCHAR({$maxlength+1}) DEFAULT {$default}`
- **required** (optional)
    - The CSV column is required input, and an empty value cause an error.
    - Do not specify **default** at the same time. Because **required** checks if the field is **''** .
    - Errors: *XXX\_required*
- **default** (optional)
    - Change the initial value actually entered in the temporary table if the value of the CSV column is empty.
    - Since it is inserted with SQL as it is, it is necessary to write "'abc'" when giving a character string.
    - e.g. `'0'`, `'NULL'`, `"'abc'"`, `'NOW()'`
- **custom** (optional)
    - Describe SQL formula to validate the field value directly.
    - Give an SQL expression so that the canonical value returns TRUE.
    - You can put all fields you set and `id` field(as CSV row number) on the expression.
    - Errors: *XXX\_custom*
    - e.g. `deleteFlag BETWEEN '0' AND '1'`
- **skipped** (optional)
    - If true, it is one of the CSV fields but not the destination table field.
    - Skipped from the transfer fields when updateExistingRecords/insertNonExistingRecords is executed.
    - This can be specified for columns that are not entered in the destination table in some way, such as user-defined columns and comment columns.

Since some validation result of CSV column values are sensitive, errors must be recognized in a fixed order.

1. required
2. maxlength
3. type
4. custom

Reference(API Doc)
------------------

[](#referenceapi-doc)

Complete Sample
---------------

[](#complete-sample)

To see *tests/test.php*

SQL actually issued by each API
-------------------------------

[](#sql-actually-issued-by-each-api)

To see *tests/QuickCsvImporterSchemaTest.php*

License
-------

[](#license)

MIT

Author
------

[](#author)

Copyright 2019 KATO Kanryu()

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

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

Total

4

Last Release

2383d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/97859f47a337faba5ea98441f4933b79a3855f44c12d880c5053f63ec27bfdf7?d=identicon)[kanryu](/maintainers/kanryu)

---

Top Contributors

[![kanryu](https://avatars.githubusercontent.com/u/759165?v=4)](https://github.com/kanryu "kanryu (37 commits)")

---

Tags

composer-packagecsvmysqlphpcsvexport-csvimport-csv

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/kanryu-quick-csv/health.svg)](https://phpackages.com/packages/kanryu-quick-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)[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.1k57.6M131](/packages/openspout-openspout)[goodby/csv

CSV import/export library

9555.6M23](/packages/goodby-csv)[sonata-project/exporter

Lightweight Exporter library

44920.9M35](/packages/sonata-project-exporter)

PHPackages © 2026

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