Share on Google+Share on Google+

The fgetcsv function example in PHP, fgetcsv php, fgetcsv in php

The example of fgetcsv() function in PHP.

Examples of fgetcsv() function in php


The PHP fgetcsv() function is used to parse the CVS file in PHP program. Here you will see how you can do so. We need a CSV file and then after opening the file we can parse using the fgetcsv() function. Here is the details of fgetcsv() function.

Syntax of fgetcsv() Funtion in PHP Program

Array fgetcsv ( file_handler [, $length [, $delimiter [, $enclosure [, string $escape ]]]] )

CSV is a type of file. Its full form is Comma Separated Values.

In Excel files can be save as .csv.

These files  opened  in any text editor shows  values as separated  with comma.

In PHP csv file can be read and displayed the values using fgetcsv() function. 

It can be achieve by using an file handler returned by fopen(), popen(), or fsockopen()) it parses each line with the  CSV fields.

It returns the CSV fields in an array with each line.

Each array is  containing the fields of each line. 

It  returns   FALSE on failure and EOF.

Code for PHP fgetcsv() Function in PHP


$file1=fopen("c:/rose1/rose2.csv","r"); //Open the CSV file

while(($array1=fgetcsv($file1,1000,","))!==FALSE) //parse file in loop



echo " ".$array1[$x];

if ($x%2==0)

echo "<br/>";







foreach ($array1 as $key=>$value)

echo " ".$value;

echo "<br/>";




roll name
1 ram
2 shyam
3 mohan
4 sita



Posted on: September 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.