PHP Classes


Recommend this page to a friend!
  Classes of Sergii Pryz   PHP CSV Import to MySQL   Download  
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP CSV Import to MySQL
Import user data from CSV files into MySQL
Author: By
Last change:
Date: 3 years ago
Size: 5,799 bytes


Class file image Download


PHP 7 ready SensioLabsInsight


Build Status Coverage Status


Build Status Coverage Status

ImportCsvUser is a console application to help import User's data from CSV (comma separated value) to MySQL. Project is working for specific CSV structure where first row acts as a schema.

Additionally ImportCsvUser has:

  • validators
  • normalizers
  • import result
  • statistics



  1. Install and run Docker container
  2. Run inside Docker container `composer install`
  3. Run inside Docker container to create database `php bin/console doctrine:database:create`


ImportCsvUser supports commands:

* user:import-csv: import csv file to database * user:age-average: get age average

_Note_: all example command SHOULD be run inside import-web container.


Please execute php bin/console user:import-csv -h to see how to use command.

Example 1

In that example is importing valid user file.

Please execute:

php bin/console user:import-csv --path ./doc/csv/user.valid.csv

As execution result:

Imported: 5
Skipped: 0

Example 2

In that example file contains some invalid rows.

Please execute command:

php bin/console user:import-csv --path ./doc/csv/user.valid.csv

As execution result:

Imported: 2
Skipped: 3
Line #1: Invalid parameter 'firstName'. This value should not be blank.
Line #1: Invalid parameter 'gender'. Choose a valid gender.
Line #5: Invalid parameter 'zipCode'. This value is too long. It should have 32 character or less.
Line #7: Invalid parameter 'birthDate'. This value is not a valid date.


Please run php bin/console user:age-average -h to see how to use command.

Example 1

Please execute command:

php bin/console user:age-average

As execution result:

Average male age: 32.12
Average female age: 30.26

File format

File SHOULD has delimiter , with enclosure ". First row in csv file is a schema. It is important the column name not an order.

Table bellow describes column name.

name/characteristics | firstname | infix | lastname | date of birth | gender | zipcode | housenumber --- | --- | --- | --- | --- | --- | --- | --- required | yes | no | no | no | no | no | no has normalizers | no | no | no | no | tim, lowercase | trim, uppercase | no data type | String[2-255] | String[0-45] | String[0-255] | Date[Y-m-d] | String['m', 'f'] | String[0-32] | String[0-255] example | Nick | ter | Tester | 1991-08-24 | m | 12010 | 9b

_Note_: column name SHOULD start form alphabetic character. It case if it's present some non alphabetic character it will be remove and application try to map columns with sanitized column name. For instance if the original column name # firstname ImportCsvUser converts it to firstname.

Skipped rows

Rows that has first column started with # are skipped.


Normalizers helps to clean users data of get the right format.

Here is a normalizer list:

  • zipcode: trim spaces, make uppercase


The data validators skip rows and write the report result with keeping process running.

There is a list of validators:

  • `firstname`: required, max length 255
  • `infix`: max length 45
  • `lastname`: max length 255
  • `gender`: `m` or `f`
  • `birthday`: date `yyyy-mm-dd`
  • `zipcode`: max length 32
  • `housenumber`: max length 255


  1. Database EER: import_csv_user.png
  2. Uml class diagram: class.diagram.png
  3. Ideas:
  4. CSV samples: csv


If you find this project worth to use please add a star. Follow changes to see all activities. And if you see room for improvement, proposals please feel free to create an issue or send pull request. Here is a great guide to start contributing.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project and its community you agree to abide by those terms.


ImportCsvUser is licensed under the MIT License. Please see the LICENSE file for details.