PHP fgetcsv() Function
Complete PHP Filesystem Reference Manual
Definition and Usage
The fgetcsv() function parses a line from an open file and checks for CSV fields.
fgetcsv() stops returning a new line when it reaches the specified length or the end of file (EOF), whichever comes first.
Returns the CSV fields as an array on success, or FALSE on failure or at the end of file (EOF).
Syntax
fgetcsv(file,length,separator,enclosure)
| Parameters | Description |
|---|---|
| file | Required. Specifies the file to check. |
| length | Optional. Specifies the maximum length of a line. It must be greater than the longest line in the CSV file. If this parameter is omitted (or set to 0), the line length is unlimited, though this may affect performance.
Note:This parameter was required in versions before PHP 5. |
| separator | Optional. Sets the field delimiter (only one character allowed). Default is a comma ( , ). |
| enclosure | Optional. Sets the field enclosure (only one character allowed). Default is a double quote ( " ). |
Tips and Notes
**Tip:** See the fputcsv() function.
Example 1
CSV file:
Kai Jim, Refsnes, Stavanger, Norway
Hege, Refsnes, Stavanger, Norway
The code above will output:
Array
(
[0] => Kai Jim
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)
Example 2
CSV file:
Kai Jim, Refsnes, Stavanger, Norway
Hege, Refsnes, Stavanger, Norway
The code above will output:
Array
(
[0] => Kai Jim
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)
Array
(
[0] => Hege
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)
Complete PHP Filesystem Reference Manual