import-CSV-in-php
 Rating: 1 Star2 Stars3 Stars4 Stars5 Stars
Loading...Loading...

How to Import CSV File In Your PHP Application



PHP gives an ease when it comes to Import flat data (Data without hierarchical structure). If you are using XML to fetch and store data that is fine but what about the case when your client have excel sheet ready with multiple records to go into database. CSV (Comma Separated Values) format is a good candidate format to import or export your application data.

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:

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 “North 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.

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 the 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. Load data command will help you here. 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. This is even more user-friendly alternative to import CSV data into a database consists in using the application “Quick CSV import with visual mapping”. 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. Following is an example of Quick CSV import

Test CSV File

Now, a working PHP example to use the above Quick CSV import Class and import the CSV File provided above

If you’ve got any thoughts, comments or suggestions for things we could add, leave a comment! Also please Subscribe to our RSS for latest tips, tricks and examples on cutting edge stuff.

Related Posts

php-domain-information

How To Get Domain, Subdomain, TLD, CTLD & Path from URL In PHP

braintree-payment-gateway-integration-php

Integrating BrainTree Payment Gateway With PHP

How to create XML with PHP Mysql

How To Create XML With PHP MySQL

RBS WorldPay Integration With PHP

Integrating RBS WorldPay XML Direct Method with PHP

  • jose vazquez

    this code doesnt work, it doesnt help with the comma issues stated at the beginning of the tutorial

  • http://www.hurricanesoftwares.com Ashish

    Can you list out what is the exact problem you have faced? The code worked fine when i used last time and it should work. Let me know what is the problem so i can dig deeper.

  • jose

    basically is that when it faces the first table column with a comma in it, everything else goes into a mess. I wont be back at work till monday since the code is over there, im already working on an alternate solution which is not necesarily badwidth friendly, but if this one works it would be great.

  • http://www.hurricanesoftwares.com Ashish

    Try the CSV given above to verify the code first before commencing with your own database. Sometimes, the problem lies in the CSV data which doesn't let it to import. Please verify that the CSV you are trying to import is correct.

  • http://www.hurricanesoftwares.com Ashish

    Try the CSV given above to verify the code first before commencing with your own database. Sometimes, the problem lies in the CSV data which doesn't let it to import. Please verify that the CSV you are trying to import is correct.

  • http://www.advancedraidrecovery.co.uk Raid Recovery

    I have tried this code for importing CSV file with php application

  • Sunil

    thanks for this code, i’ll use it to import too.

  • Omar_chavez

    EXCELENT!!! Fantastic!!! Wonderfull…nnjejejejejeje this script gives to me a idea … i’ll try another task, to make a data struct … (sorry, u00a0my english is bad…) …The idea is… from CSV file, make a List of data Structures with Fields and Rows, like DataReader… or … Data Entity ….ok… if i have the next file:nnCustomers.csvnID, NAME, AGEn0,JONATHAN,32n1,MARIA,23n2,JHONY,24nnI’ll make the next struct in memory:nnCostomers[0][“ID”] = 0;Costomers[0][“NAME”] = “JONATHAN”;Costomers[0][“AGE”] = 32;Costomers[1][“ID”] = 1;Costomers[1][“NAME”] = “maria”;Costomers[1][“AGE”] = 23;Costomers[2][“ID”] = 2;Costomers[3][“NAME”] = “JHONY”;Costomers[4][“AGE”] = 24;…. mmmmm Maebe this could be an Object.,,,,…u00a0nnokok … just an array…. the next step will be to make it an object… :)nnIf you want to know if I’ll Do’it… find me in omar_chavez hotmail com … addme in the MSN u00a0:)

  • Freddy K

    Great work – thanks! It would be perfect if it was platform independant though. Not everybody uses a MySQL database with PHP.

  • http://www.register-domainname.in/ Buy Domain Names

    I found your post as a great source of valuable information..

  • Michael

    Dose not work, if it’s ; Separatet

  • sadho
  • sadho