Rating: 1 Star2 Stars3 Stars4 Stars5 Stars

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.

  • jose vazquez

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

  • 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.

  • 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.

  • 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.

  • 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.

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

  • Michael

    Dose not work, if it’s ; Separatet

  • sadho
  • sadho
  • Nadia

    Great script, however I can’t get it to work. 🙁 I keep getting the following error msg, despite having full database privilleges. “Errors: Access denied for user…” Interestingly it creates the table with the field headings, just doesnt populate the table with content.

  • This kind of error clearly states that the problem lies with the access credentials which is not allowing the script to run or make modifications in tables.

    Please check that you have proper MySQL access rights (privileges) before you continue.

  • Alex Skakunov

    It’s nice that you use my class in this article. Can I ask you add a back link to my blog?

    Quick CSV import with visual mapping

  • Thanks Alex, you have already mentioned that in your comment.

  • Pewe


    Just the script I need, but –

    – Where is the table specified for the storing of data in the uploaded csv

    – Initially connects to database but returns permission error when file is uploaded

    “Errors: Access denied for user ‘webi8726’@’localhost’ (using password: YES)”

    similar to previous commenters problem -BUT user has full database rights – and other scripts using the database work.

    Would be grateful for any info.


  • Ramesh

    Excellent Tutorial, is there a way to validate the column before uploading?

  • xuqi

    Perfect code – Thanks for help

  • adrian

    Great script. The script doesn’t remove field enclosures in the headers. This code may work:

    if (empty($this->arr_csv_columns)) {
    } else {
    $this->arr_csv_columns = array_map(array($this,’d_escape’), $this->arr_csv_columns);

    function d_escape($arr) {

  • Mac

    Notice: Undefined variable: HTTP_POST_FILES in C:xampphtdocs1Csv.php on line 18

    Warning: fopen(): Filename cannot be empty in C:xampphtdocs1Quick_CSV_import.php on line 65

    Warning: fopen(): Filename cannot be empty in C:xampphtdocs1Quick_CSV_import.php on line 65

    I get this warning, I just copied the codes into xampp htdocs folder and then ran it.

  • Mac


  • Enmanuel Corvo

    Easy way to export CSV in PHP: