Quickly importing data from CSV file into PHP applications

Did you know there are data exchange formats besides XML? If the data to exchange is flat, i.e. is not in an hierarchical structure, CSV (comma separated values) format is a good candidate format to import or your export your application data.

You can find out more about CSV format in Wikipedia:


* Importing data from CSV files

Since we have lines of values separated by commas, the easiest way to process them is to parse each line using the PHP explode() function:


$arrResult = array();
$arrLines = file('data.csv');
foreach($arrLines as $line) {
$arrResult[] = explode( ',', $line);


* Dealing with special characters

This simple solution will not work if you have a comma in a value, like for instance when the column is an address and it has a value is like "Obama street, 1".

In such cases, the column value in the CSV file is quoted to indicate that the data between quotes should be read as a single column.

To deal with this situation, you can use a specially tailored regular expression or use the PHP fgetcsv() function.



$arrResult = array();
$handle = fopen("data.csv", "r");
if( $handle ) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$arrResult[] = $data;

Note that locale settings may affect how fgetcsv() function works. As noted in the PHP manual, if LANG locale setting is for instance "en_US.UTF-8", files in one-byte encoding are read wrong by this function, so be aware.

* Importing CSV data directly into a database

If you are importing data from a CSV file into a MySQL database, you import it directly into a database table. It will be much faster than processing it line by line using PHP.

Fortunately, most relational database have tools for the bulk data import from CSV files. For instance, in MySQL you can use LOAD DATA INFILE query statement.


* Friendly user interface to import CSV data into a database

An alternative solution to import CSV data is to use my class "Quick CSV Import" also available in the PHPClasses site:


An even more user-friendly alternative to import CSV data into a database consists in using the application "Quick CSV import with visual mapping". It is based in the class above.

This application helps importing CSV files into a database table allowing to define with CSV file columns are mapped to which columns of the database table. This application can even suggest automatically separator character besides the comma.

You may learn more about this application in the following page:






© 2008 GZMweb.com. All rights reserved. Privacy policy | Terms of use