2 Test fgetcsv() : usage variations - with file handle and length, file pointer pointing at end of file
6 Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] );
7 Description: Gets line from file pointer and parse for CSV fields
11 Testing fgetcsv() to read a file whose file pointer is pointing to end of file
12 and fgetcsv() provided with file handle and length arguments
15 echo "*** Testing fgetcsv() : with file handle and length arguments, file pointer pointing at end of file ***\n";
17 /* the array is with two elements in it. Each element should be read as
18 1st element is delimiter & 2nd element is csv fields
21 array(',', 'water,fruit'),
22 array(' ', 'water fruit'),
23 array(' ', '"water" "fruit"'),
24 array('\\', 'water\\"fruit"\\"air"'),
25 array('\\', '"water"\\"fruit"\\"""'),
28 $filename = dirname(__FILE__) . '/fgetcsv_variation30.tmp';
31 $file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
37 foreach ($csv_lists as $csv_list) {
38 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
39 // create the file and add the content with has csv fields
40 if ( strstr($file_modes[$mode_counter], "r") ) {
41 $file_handle = fopen($filename, "w");
43 $file_handle = fopen($filename, $file_modes[$mode_counter] );
45 if ( !$file_handle ) {
46 echo "Error: failed to create file $filename!\n";
49 $delimiter = $csv_list[0];
50 $csv_field = $csv_list[1];
52 fwrite($file_handle, $csv_field . "\n");
53 // write another line of text and a blank line
54 // this will be used to test, if the fgetcsv() read more than a line and its
55 // working when only a blan line is read
56 fwrite($file_handle, "This is line of text without csv fields\n");
57 fwrite($file_handle, "\n"); // blank line
59 // close the file if the mode to be used is read mode and re-open using read mode
60 if ( strstr($file_modes[$mode_counter], "r" ) ) {
62 $file_handle = fopen($filename, $file_modes[$mode_counter]);
65 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
67 // set the file pointer to EOF
68 var_dump( fseek($file_handle, 0, SEEK_END) );
70 // call fgetcsv() to parse csv fields
72 // now file pointer should point to end of the file, try reading again
73 var_dump( feof($file_handle) );
74 var_dump( fgetcsv($file_handle, 1024) );
75 // check the file pointer position and if eof
76 var_dump( ftell($file_handle) );
77 var_dump( feof($file_handle) );
88 *** Testing fgetcsv() : with file handle and length arguments, file pointer pointing at end of file ***
90 -- Testing fgetcsv() with file opened using r mode --
97 -- Testing fgetcsv() with file opened using rb mode --
104 -- Testing fgetcsv() with file opened using rt mode --
111 -- Testing fgetcsv() with file opened using r+ mode --
118 -- Testing fgetcsv() with file opened using r+b mode --
125 -- Testing fgetcsv() with file opened using r+t mode --
132 -- Testing fgetcsv() with file opened using a+ mode --
139 -- Testing fgetcsv() with file opened using a+b mode --
146 -- Testing fgetcsv() with file opened using a+t mode --
153 -- Testing fgetcsv() with file opened using w+ mode --
160 -- Testing fgetcsv() with file opened using w+b mode --
167 -- Testing fgetcsv() with file opened using w+t mode --
174 -- Testing fgetcsv() with file opened using x+ mode --
181 -- Testing fgetcsv() with file opened using x+b mode --
188 -- Testing fgetcsv() with file opened using x+t mode --
195 -- Testing fgetcsv() with file opened using r mode --
202 -- Testing fgetcsv() with file opened using rb mode --
209 -- Testing fgetcsv() with file opened using rt mode --
216 -- Testing fgetcsv() with file opened using r+ mode --
223 -- Testing fgetcsv() with file opened using r+b mode --
230 -- Testing fgetcsv() with file opened using r+t mode --
237 -- Testing fgetcsv() with file opened using a+ mode --
244 -- Testing fgetcsv() with file opened using a+b mode --
251 -- Testing fgetcsv() with file opened using a+t mode --
258 -- Testing fgetcsv() with file opened using w+ mode --
265 -- Testing fgetcsv() with file opened using w+b mode --
272 -- Testing fgetcsv() with file opened using w+t mode --
279 -- Testing fgetcsv() with file opened using x+ mode --
286 -- Testing fgetcsv() with file opened using x+b mode --
293 -- Testing fgetcsv() with file opened using x+t mode --
300 -- Testing fgetcsv() with file opened using r mode --
307 -- Testing fgetcsv() with file opened using rb mode --
314 -- Testing fgetcsv() with file opened using rt mode --
321 -- Testing fgetcsv() with file opened using r+ mode --
328 -- Testing fgetcsv() with file opened using r+b mode --
335 -- Testing fgetcsv() with file opened using r+t mode --
342 -- Testing fgetcsv() with file opened using a+ mode --
349 -- Testing fgetcsv() with file opened using a+b mode --
356 -- Testing fgetcsv() with file opened using a+t mode --
363 -- Testing fgetcsv() with file opened using w+ mode --
370 -- Testing fgetcsv() with file opened using w+b mode --
377 -- Testing fgetcsv() with file opened using w+t mode --
384 -- Testing fgetcsv() with file opened using x+ mode --
391 -- Testing fgetcsv() with file opened using x+b mode --
398 -- Testing fgetcsv() with file opened using x+t mode --
405 -- Testing fgetcsv() with file opened using r mode --
412 -- Testing fgetcsv() with file opened using rb mode --
419 -- Testing fgetcsv() with file opened using rt mode --
426 -- Testing fgetcsv() with file opened using r+ mode --
433 -- Testing fgetcsv() with file opened using r+b mode --
440 -- Testing fgetcsv() with file opened using r+t mode --
447 -- Testing fgetcsv() with file opened using a+ mode --
454 -- Testing fgetcsv() with file opened using a+b mode --
461 -- Testing fgetcsv() with file opened using a+t mode --
468 -- Testing fgetcsv() with file opened using w+ mode --
475 -- Testing fgetcsv() with file opened using w+b mode --
482 -- Testing fgetcsv() with file opened using w+t mode --
489 -- Testing fgetcsv() with file opened using x+ mode --
496 -- Testing fgetcsv() with file opened using x+b mode --
503 -- Testing fgetcsv() with file opened using x+t mode --
510 -- Testing fgetcsv() with file opened using r mode --
517 -- Testing fgetcsv() with file opened using rb mode --
524 -- Testing fgetcsv() with file opened using rt mode --
531 -- Testing fgetcsv() with file opened using r+ mode --
538 -- Testing fgetcsv() with file opened using r+b mode --
545 -- Testing fgetcsv() with file opened using r+t mode --
552 -- Testing fgetcsv() with file opened using a+ mode --
559 -- Testing fgetcsv() with file opened using a+b mode --
566 -- Testing fgetcsv() with file opened using a+t mode --
573 -- Testing fgetcsv() with file opened using w+ mode --
580 -- Testing fgetcsv() with file opened using w+b mode --
587 -- Testing fgetcsv() with file opened using w+t mode --
594 -- Testing fgetcsv() with file opened using x+ mode --
601 -- Testing fgetcsv() with file opened using x+b mode --
608 -- Testing fgetcsv() with file opened using x+t mode --