PHP fgetcsv() Function
❮ PHP Filesystem ReferenceExample
Read and output one line from the open CSV file:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
Run Example »
Definition and Usage
The fgetcsv() function parses a line from an open file, checking for CSV fields.
Tip: Also see the fputcsv() function.
Syntax
fgetcsv(file, length, separator, enclosure)
Parameter Values
Parameter | Description |
---|---|
file | Required. Specifies the open file to return and parse a line from |
length | Optional. Specifies the maximum length of a line. Must be greater than the longest line (in characters) in the CSV file. Omitting this parameter (or setting it to 0) the line length is not limited, which is slightly slower. Note: This parameter is required in versions prior to PHP 5 |
separator | Optional. Specifies the field separator. Default is comma ( , ) |
enclosure | Optional. Specifies the field enclosure character. Default is " |
escape | Optional. Specifies the escape character. Default is "\\" |
Technical Details
Return Value: | An array with the CSV fields on success, NULL if an invalid file is supplied, FALSE on other errors and on EOF |
---|---|
PHP Version: | 4.0+ |
Binary Safe: | Yes, in PHP 4.3.5 |
PHP Changelog: | PHP 5.3 - Added the escape parameter |
More Examples
Example
Read and output the entire contents of a CSV file:
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
Run Example »
❮ PHP Filesystem Reference