Read data from a CSV file in PHP

// 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);

33 Replies to “Read data from a CSV file in PHP”

  1. 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. 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);

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

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

  5. @santhi,
    >>>how you decided to 5 rows ???
    Hm…….. This is a sample one.
    It should be taken as example. For the example sake, I’ve put as 5 rows.

    >>>>>r u programmer?
    what made you think not so?!

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

  7. @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?


  8. 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 ?

  9. Hi again 🙂

    I resolve this by my self 😀
    $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 😀

  10. @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”){


    Let me know if this works.

  11. <?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] . "”;


    With this code, I’ve 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?

  12. I have read this script and impliment it in our script but i have used this script for php data searching in csv file by inpt field but in this code i have facing more problem and ihave get the repited error message if records not found in csv file.

    so please let me know if u find any solution on this code

    thanks in advance


  13. I want user to input a file ..I want them to restrict the csv/txt file to 5 fields, how can I write a code for that in php

    Many thanks in advance

  14. hello sir , i have one problem , in my site i have use code to read .xls file and match it with database field and perform updation , all goes right when file is .xls, but when i m going to use csv file it shows error like “file not readable” can u help me???????????

  15. I tried the script and perfectly reading a 43MB csv file. But after in a couple of minutes the browser crash in the middle of the processing..

    I’m having about 500k rows of record. Any idea of the problem?

Leave a Reply to sourabh Cancel reply

Your email address will not be published. Required fields are marked *