1--TEST--
2Test fgetcsv() : usage variations - reading the blank line
3--FILE--
4<?php
5/*
6 Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] );
7 Description: Gets line from file pointer and parse for CSV fields
8*/
9
10/* Testing fgetcsv() by reading a file containing a blank line */
11
12echo "*** Testing fgetcsv() : reading the blank line ***\n";
13
14
15$filename = __DIR__ . '/fgetcsv_variation14.tmp';
16@unlink($filename);
17
18$file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
19                     "a+", "a+b", "a+t",
20                     "w+", "w+b", "w+t",
21                     "x+", "x+b", "x+t");
22
23$loop_counter = 1;
24  for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
25    // create the file and add the content with has csv fields
26    if ( strstr($file_modes[$mode_counter], "r") ) {
27      $file_handle = fopen($filename, "w");
28    } else {
29      $file_handle = fopen($filename, $file_modes[$mode_counter] );
30    }
31    if ( !$file_handle ) {
32      echo "Error: failed to create file $filename!\n";
33      exit();
34    }
35    // write a blank line
36    fwrite($file_handle, "\n"); // blank line
37
38    // close the file if the mode to be used is read mode  and re-open using read mode
39    // else rewind the file pointer to beginning of the file
40    if ( strstr($file_modes[$mode_counter], "r" ) ) {
41      fclose($file_handle);
42      $file_handle = fopen($filename, $file_modes[$mode_counter]);
43    } else {
44      // rewind the file pointer to bof
45      rewind($file_handle);
46    }
47
48    echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
49
50    // call fgetcsv() to parse csv fields
51
52    // read the next line which is a blank line to see the working of fgetcsv
53    $fp_pos = ftell($file_handle);
54    var_dump( fgetcsv($file_handle, 1024) );
55    // check the file pointer position and if eof
56    var_dump( ftell($file_handle) );
57    var_dump( feof($file_handle) );
58    // read again to struck EOF
59    var_dump( fgetcsv($file_handle, 1024) );
60    // check the file pointer position and if eof
61    var_dump( ftell($file_handle) );
62    var_dump( feof($file_handle) );
63
64    // close the file
65    fclose($file_handle);
66    //delete file
67    unlink($filename);
68  } //end of mode loop
69
70echo "Done\n";
71?>
72--EXPECT--
73*** Testing fgetcsv() : reading the blank line ***
74
75-- Testing fgetcsv() with file opened using r mode --
76array(1) {
77  [0]=>
78  NULL
79}
80int(1)
81bool(false)
82bool(false)
83int(1)
84bool(true)
85
86-- Testing fgetcsv() with file opened using rb mode --
87array(1) {
88  [0]=>
89  NULL
90}
91int(1)
92bool(false)
93bool(false)
94int(1)
95bool(true)
96
97-- Testing fgetcsv() with file opened using rt mode --
98array(1) {
99  [0]=>
100  NULL
101}
102int(1)
103bool(false)
104bool(false)
105int(1)
106bool(true)
107
108-- Testing fgetcsv() with file opened using r+ mode --
109array(1) {
110  [0]=>
111  NULL
112}
113int(1)
114bool(false)
115bool(false)
116int(1)
117bool(true)
118
119-- Testing fgetcsv() with file opened using r+b mode --
120array(1) {
121  [0]=>
122  NULL
123}
124int(1)
125bool(false)
126bool(false)
127int(1)
128bool(true)
129
130-- Testing fgetcsv() with file opened using r+t mode --
131array(1) {
132  [0]=>
133  NULL
134}
135int(1)
136bool(false)
137bool(false)
138int(1)
139bool(true)
140
141-- Testing fgetcsv() with file opened using a+ mode --
142array(1) {
143  [0]=>
144  NULL
145}
146int(1)
147bool(false)
148bool(false)
149int(1)
150bool(true)
151
152-- Testing fgetcsv() with file opened using a+b mode --
153array(1) {
154  [0]=>
155  NULL
156}
157int(1)
158bool(false)
159bool(false)
160int(1)
161bool(true)
162
163-- Testing fgetcsv() with file opened using a+t mode --
164array(1) {
165  [0]=>
166  NULL
167}
168int(1)
169bool(false)
170bool(false)
171int(1)
172bool(true)
173
174-- Testing fgetcsv() with file opened using w+ mode --
175array(1) {
176  [0]=>
177  NULL
178}
179int(1)
180bool(false)
181bool(false)
182int(1)
183bool(true)
184
185-- Testing fgetcsv() with file opened using w+b mode --
186array(1) {
187  [0]=>
188  NULL
189}
190int(1)
191bool(false)
192bool(false)
193int(1)
194bool(true)
195
196-- Testing fgetcsv() with file opened using w+t mode --
197array(1) {
198  [0]=>
199  NULL
200}
201int(1)
202bool(false)
203bool(false)
204int(1)
205bool(true)
206
207-- Testing fgetcsv() with file opened using x+ mode --
208array(1) {
209  [0]=>
210  NULL
211}
212int(1)
213bool(false)
214bool(false)
215int(1)
216bool(true)
217
218-- Testing fgetcsv() with file opened using x+b mode --
219array(1) {
220  [0]=>
221  NULL
222}
223int(1)
224bool(false)
225bool(false)
226int(1)
227bool(true)
228
229-- Testing fgetcsv() with file opened using x+t mode --
230array(1) {
231  [0]=>
232  NULL
233}
234int(1)
235bool(false)
236bool(false)
237int(1)
238bool(true)
239Done
240