3 Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] );
4 Description: Gets line from file pointer and parse for CSV fields
8 Testing fgetcsv() to read a file whose file pointer is pointing to end of file
9 and fgetcsv() provided with only file handle in its argument
11 <<__EntryPoint
>> function main(): void
{
12 echo "*** Testing fgetcsv() : with file handle as only argument and file pointer pointing at end of file ***\n";
14 /* the array is with two elements in it. Each element should be read as
15 1st element is delimiter & 2nd element is csv fields
18 varray
[',', 'water,fruit'],
19 varray
[' ', 'water fruit'],
20 varray
[' ', '"water" "fruit"'],
21 varray
['\\', 'water\\"fruit"\\"air"'],
22 varray
['\\', '"water"\\"fruit"\\"""'],
25 $filename = __SystemLib\
hphp_test_tmppath('fgetcsv_variation29.tmp');
28 $file_modes = varray
["r","rb", "rt", "r+", "r+b", "r+t",
34 foreach ($csv_lists as $csv_list) {
35 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++
) {
36 // create the file and add the content with has csv fields
37 if ( strstr($file_modes[$mode_counter], "r") ) {
38 $file_handle = fopen($filename, "w");
40 $file_handle = fopen($filename, $file_modes[$mode_counter] );
42 if ( !$file_handle ) {
43 echo "Error: failed to create file $filename!\n";
46 $delimiter = $csv_list[0];
47 $csv_field = $csv_list[1];
49 fwrite($file_handle, $csv_field . "\n");
50 // write another line of text and a blank line
51 // this will be used to test, if the fgetcsv() read more than a line and its
52 // working when only a blan line is read
53 fwrite($file_handle, "This is line of text without csv fields\n");
54 fwrite($file_handle, "\n"); // blank line
56 // close the file if the mode to be used is read mode and re-open using read mode
57 // else rewind the file pointer to beginning of the file
58 if ( strstr($file_modes[$mode_counter], "r" ) ) {
60 $file_handle = fopen($filename, $file_modes[$mode_counter]);
63 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
65 // set the file pointer to EOF
66 var_dump( fseek($file_handle, 0, SEEK_END
) );
68 // call fgetcsv() to parse csv fields
70 // now file pointer should point to end of the file, try reading again
71 var_dump( feof($file_handle) );
72 var_dump( fgetcsv($file_handle) );
73 // check the file pointer position and if eof
74 var_dump( ftell($file_handle) );
75 var_dump( feof($file_handle) );