PHP CSV File Read

This tutorial is to help you to read a CSV file or data. Character Separated Values(CSV) or Comma Separated Values is a file type containing plain text content with comma or a character as separators. It is a convenient form to store simple data. PHP has two in-built functions to read CSV file.

  1. fgetcsv() – Reads CSV using the reference of the file resource.
  2. str_getcsv() – Reads CSV data stored in a variable.

 php_csv_file_read

CSV File Read using PHP fgetcsv()

This function accepts CSV file handle as mandatory argument. It reads CSV data and converts it into array. The syntax is,

fgetcsv($file_handle,$limit,$separator,$enclosure,$escape_character)
Parameters Description
$file_handle CSV file resource data.
$limit Max length for reading a line of data.
$separator Character delimiter.
$enclosure Character used to enclose values
$escape_character Escape character while reading CSV.

Note:

  • If $file_handle is not a valid resource, then fgetcsv() will return NULL.
  • By specifying $limit parameter, it will speed up the execution of this function.

CSV File Read Example

First, we have to create a CSV file handle by opening required CSV file. The CSV file we have used for this example is,

fruits.csv

"Apple","RED","#D62433"
"Orange","ORANDE","#FEB635"
"Banana","YELLOW","#FEE492"
"Grapes","VIOLET","#B370AD"
"KIWI","GREEN","#9BA207"
"Dates","BROWN","#922E2F"

In the above CSV file, it has Comma(,) as separator and Double Quotes(“) as enclosure.

The CSV file contains fruits table records. Values represent name, color and color code of the fruits. The PHP script to read this file is,

<?php
$CSVfp = fopen("fruits.csv", "r");
if($CSVfp !== FALSE) {
 while(! feof($CSVfp)) {
  $data = fgetcsv($CSVfp, 1000, ",");
  print_r($data);
 }
}
fclose($CSVfp);
?>

In this script we are invoking fgetCSV() by moving CSV file pointer until it reaches end of line. Each time, it converts current line of data into an array.

PHP CSV String Data Read

The function str_getcsv() is for reading CSV from string data instead of file resource. The syntax is,

str_getcsv($csv_string,$separator,$enclosure,$escape_character)

The first argument is used to pass CSV string data to this function. Rest of the arguments are for the same purpose as we have seen in fgetcsv(). In this function, there is no limit for reading line of CSV string.

str_getcsv() Example

First we have to store fruits.csv file content into a variable and pass it into str_getcsv(). And then, this function is called for separating lines with appropriate line breaks. While iterating among these lines it will be further separated into fields. The script is,

<?php
$str_CSV = '"Apple","RED","#D62433"
"Orange","ORANDE","#FEB635"
"Banana","YELLOW","#FEE492"
"Grapes","VIOLET","#B370AD"
"KIWI","GREEN","#9BA207"
"Dates","BROWN","#922E2F"';

$row = str_getcsv($str_CSV, "\n");
$length = count($row);
for($i=0;$i<$length;$i++) {
 $data = str_getcsv($row[$i], ",");
 print_r($data);
}	
?>

Output:

We will get same output for both examples.

Array([0] => Apple [1] => RED [2] => #D62433)
Array([0] => Orange [1] => ORANDE [2] => #FEB635)
Array([0] => Banana [1] => YELLOW [2] => #FEE492)
Array([0] => Grapes [1] => VIOLET [2] => #B370AD)
Array([0] => KIWI [1] => GREEN [2] => #9BA207)
Array([0] => Dates [1] => BROWN [2] => #922E2F)

By iterating this array, we can handle these data in variety of ways. For example, displaying CSV in grid view or creating query statements and etc.

Displaying CSV Data in Grid View

CSV_grid_view

Creating Queries using CSV Data

query_from_csv

Download PHP CSV File Read Source Code

This PHP code tutorial was published on April 4, 2014.

↑ Back to Top