English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Example of PHP fgetcsv() Function

PHP Filesystem Reference Manual

The fgetcsv() function can parse a line and parse CSV fields from an open file. This function stops returning a new line with the specified length or EOF, whichever comes first. On success, it returns an array of CSV fields, or false on failure and EOF.

Syntax

array fgetcsv ( resource $handle[, int $length = 0[, string $delimiter = ","[, string $enclosure = '"'[, string $escape = "\\"]]]] )

This function is similar to the fgets() function, but the difference is that the fgetcsv() function parses the line of CSV fields read and returns an array containing the read fields. The fgetcsv() function can return false when an error occurs (including the end of the file).

Example1

<?php
   $file = fopen("/PhpProject/EmpDetails.csv, "r"); 
   echo fgetcsv($file);
   fclose($file);
?>

Output Result

Array
(
   [0] => Chandra
   [1] => Ravi
   [2] => Adithya
   [3] => Sai
)

Example2

<?php
   $file = fopen("/PhpProject/EmpDetails.csv, "r"); 
   while(! feof($file)) {
      print_r(fgetcsv($file));
   }
   fclose($file);
?>

Output Result

Array
(
    [0] => Chandra
    [1] => Ravi
    [2] => Adithya
    [3] => Sai
)
Array
(
    [0] => Dev
    [1] => Jai
    [2] => Ramesh
    [3] => Raja
)

PHP Filesystem Reference Manual