Read and display one line from the open CSV file.
<?php $file = fopen(“contacts.csv”,“r”); print_r(fgetcsv($file)); fclose($file); ?> |
The fgetcsv() function parses a line from an open file and identifies CSV fields.
Tip: Also check out the fputcsv() function.
fgetcsv(file, length, separator, enclosure) |
Parameter |
Description |
file |
Mandatory. Specifies the open file from which to read and parse a line. |
length |
Optional. Defines the maximum length of a line, which should be longer than the longest line (in characters) in the CSV file. If omitted or set to 0, there is no length limit, which may result in slightly slower performance. Note: This parameter was required in versions prior to PHP 5. |
separator |
Optional. Defines the field separator, with a default of a comma ( |
enclosure |
Optional. Defines the field enclosure character, with a default of a double quote ( |
escape |
Optional. Defines the escape character, with a default of a backslash ( |
Return Value: |
Returns an array with the CSV fields on success, |
PHP Version: |
4.0+ |
Binary Safe: |
Yes, in PHP 4.3.5 |
PHP Changelog: |
PHP 5.3 introduced the |
Read and display the complete contents of a CSV file.
<?php $file = fopen(“contacts.csv”,“r”); while(! feof($file)) { print_r(fgetcsv($file)); } fclose($file); ?> |