Back 21 Comments

Tutorial: Import a CSV File Using PHP and MySQL

The following code should help beginner PHP/MySQL developers who are looking for an easy way to import a CSV or comma delimited file into a mysql database.  This example adds new contacts into the contacts table from an uploaded CSV file, populating the following three fields: (contact_first, contact_last, contact_email).

Click to view files:

I've included the PHP code below that 'does all the work'.  Click the 'import.php' link above to view the full code.

    //get the csv file
    $file = $_FILES[csv][tmp_name];
    $handle = fopen($file,"r");
    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysql_query("INSERT INTO contacts_tmp (contact_first, contact_last, contact_email) VALUES
    } while ($data = fgetcsv($handle,1000,",","'"));

February 15, 2013 9:33am
thank you for sample
September 19, 2012 2:57am
hi friend your code very nice but you use to fopen and i need without fopen so pls with out fopen import file phpmysql data base
November 10, 2012 4:12am
it is not working properly
September 20, 2012 11:02am
thanks. works great.
Prove the code and it works!!!
October 10, 2012 3:16pm
Hey man i got an error, it says, $data is undefined, it works perfect in localhost, but in a production website throws this error.
October 17, 2012 2:22am
hi there,
i am having a problem here.. the ERR is

Notice: Use of undefined constant csv - assumed 'csv' in C:\xampp\htdocs\index.php on line 8

Notice: Use of undefined constant size - assumed 'size' in C:\xampp\htdocs\index.php on line 8

Notice: Undefined index: csv in C:\xampp\htdocs\index.php on line 8

can you please check it out...

Ratan Thapa
Babulal Saini
October 18, 2012 1:38am
This is very nice.....
October 25, 2012 8:43am
hey great tutorial, works excellent. Though it took me a while to code the error checking and validation I needed, this got me off to a great start, as someone new to PHP and MySQL, I really appreciate this.
November 1, 2012 2:47pm
can u also provide the similar script for codeigniter?
November 18, 2012 2:08pm
hello,can you tell my why i got the object not found error??many thx
November 21, 2012 1:15am
wrk fine..
November 23, 2012 6:52am
works fine :)
December 8, 2012 8:55am
i have more than one i want it can browse any how i change this rows..
do {
if ($data[0]) {
mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
} while ($data = fgetcsv($handle,1000,",","'"));

the statement "INSERT INTO contacts (contact_first, contact_last, contact_email) mean only can select one table from contact right?
December 14, 2012 3:31am
It's working fine at all.

Saurabh Kumar
(software developer)
January 9, 2013 1:26pm
Awesome, that works! Thanks a lot.
January 10, 2013 2:19am
Can you also post importing .csv file using codeignite?please
January 10, 2013 2:22am
Can you also post importing .csv file using codeigniter? this code works but i need to use implement it using codeigniter!please
sourabh kasliwal
January 31, 2013 12:48am
very nice simple csv read write code i have found on
February 10, 2013 11:18pm
i use(import.php) code its working fine thank you
February 26, 2013 2:08am
Working fine...
March 7, 2013 9:07am
Thank you for sharing

Shame on some of you with silly questions, with very simple code, if you cant debug, dont use more PHP, for your security !

Your comment has been posted....