Anda di halaman 1dari 7

Contents * Introduction * Importing data from CSV files * Dealing with special characters * Importing CSV data directly

into a database * Friendly user interface to import CSV data into a database * Introduction
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:
http://en.wikipedia.org/wiki/CSV

* 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: <?php $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.

http://www.php.net/fgetcsv

<?php $arrResult = array(); $handle = fopen("data.csv", "r"); if( $handle ) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $arrResult[] = $data; } fclose($handle); } ?> 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.
http://dev.mysql.com/doc/refman/5.0/en/load-data.html

* 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:
http://www.phpclasses.org/quick_csv_import

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:
http://i1t2b3.com/2009/01/14/quick-csv-import-with-mapping/

Read data from a CSV file in PHP

Written by Kumar S

Posted November 4, 2006 at 8:00 am // open the text file $fd = fopen ("pilots.csv", "r"); // initialize a loop to go through each line of the file while (!feof ($fd)) { $buffer = fgetcsv($fd, 4096); // declare an array to hold all of the contents of each //row, indexed echo "n"; // this for loop to traverse thru the data cols // when this is re-created with MySQL use the mysql_num_fileds() function to get // this number for ($i = 0; $i < 5; ++$i){ if ($buffer[$i] == ""){ $buffer[$i] = " "; } // print 's with each index echo "$buffer[$i]n"; } echo "n"; } fclose ($fd); ?>

20 Responses to this post 1. shoni on March 31st, 2007 12:16 pm


Sir I am facing some problems when I apply your Read data from a CSV file in PHP code, my browser hangs and displays lots of errors in these two lines while (!feof ($fd)) { $buffer = fgetcsv($fd, 4096); . Kindly help me in this.

2. Kumar S on April 15th, 2007 12:58 pm


how large is you csv file? can you please paste you code you were using as well as the data file also.

3. Joan on June 4th, 2007 12:01 pm


I am facing some problems when I apply your Read data from a CSV file in PHP code, my browser hangs and displays lots of errors in these two lines while (!feof ($fd)) { $buffer = fgetcsv($fd, 4096);

4. Tejas on June 13th, 2007 10:48 am


Hi all, sorry to interrupt ur conversation..I am using the above code to read the data from a CSV. It is reading fine, except it also prints the column name, which i dont want. I tried changing the the for loop so that it reads from 2nd row where my actual data starts. It didnt work, changing the i value there does not display the data at all suggest please

5. ronald on November 10th, 2007 1:38 am


works perfectly !!! thanks

6. santhi on May 15th, 2008 3:02 pm


how you decided to 5 rows ??? r u programmer?

7. Peter Mansell on May 28th, 2008 6:21 pm


just to say you can use:

for ($i = 0; $i < count($buffer); ++$i)


to count the rows in the CSV and iterate round the dynamic amount of rows. Its better than using the fixed number as sometimes you may want to count a changing CSV file.

8. Kumar S on June 29th, 2008 6:38 am


@santhi, >>>how you decided to 5 rows ??? Hm.. This is a sample one. It should be taken as example. For the example sake, Ive put as 5 rows. >>>>>r u programmer? what made you think not so?!

9. Rajeev on March 29th, 2009 5:14 am


Peter it works fine with for ($i = 0; $i < count($buffer); ++$i) I want to use a csv file data a value stored in a particular row and column and use this value as a variable. How can I do this

10. Kumar S on March 30th, 2009 10:55 am


You want to read a particular row and column ( a cell) value and then store it in a variable? Can u explain in detail?

11. Umar on September 7th, 2009 5:31 pm


Great Script, thanks

12. Umar on September 7th, 2009 5:32 pm

GREAT SCRIPT, THANK

13. Harleen singh on March 9th, 2010 12:06 pm


@Kumar, its nice script. it outputs first and second column by concatenating but i want to read first and second column separately. what will be the code for this? Harleen

14.goran298 on March 19th, 2010 1:10 pm


Hi. My test.csv file look like this. Header: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 row1: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 how can i get this script to name col1 to an variabel, like this: $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15 ?

15. goran298 on March 19th, 2010 5:00 pm


Hi again I resolve this by my self <?php $handle = fopen("test.csv", "r"); $row = 1; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "$data[2]","$data[4]","$data[0]"; echo "; } ?> Now can I make a new CSV-file

16. Kumar S on March 20th, 2010 6:12 am


@goran298 great. you can create a csv file, by using normal file operations. you can do like this first create the contents like this $heading = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 $contents = $heading; $contents .= 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; $fh = fopen(test.csv,wb){ fwrite($fh,$contents); } fclose($fh); Let me know if this works.

17. goran298 on March 20th, 2010 11:11 am


<?php

$file_handle = fopen("Mai2010.csv", "r"); while (!feof($file_handle) ) { $data = fgetcsv($file_handle, 1024); print $data[0] . $data[1]. $data[2] . $data[3] . $data[4] . $data[5] . $data[6] . $data[7] . $data[8] . $data[9] . $data[10] . $data[11] . $data[12] . $data[13] . $data[14] . "; } fclose($file_handle); ?> With this code, Ive been shown the CSV file so I will. My next step is to get changed $data[0], $data[1] and so on, for example, $Flight $Plane. How can one do this?

18.sucheta on May 6th, 2010 9:49 am


I want to read a csv file. and data to store in a particular row and column . How can I do this

19. Kumar S on May 6th, 2010 10:32 am


You can write into csv files using fputcsv function in php. But you have to write your own custom function to insert into a particular row and column. for further details see this http://www.php.net/manual/en/function.fputcsv.php. Let me know if this helps

20. Kumar S on May 6th, 2010 10:38 am


another function available in PHP. str_getcsv http://php.net/manual/en/function.str-getcsv.php what comes immediately to my mind is, you have to read the csv file into an array. then make changes to the row/cols in that array. then just write again into the csv file. that will work as you expect.

Anda mungkin juga menyukai