1--TEST--
2Test fgetcsv() : usage variations - reading files opened in write only mode (Bug #42036)
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() to read from files opened in write only mode */
11
12echo "*** Testing fgetcsv() : reading the files opened in write only mode ***\n";
13
14/* the array is with three elements in it. Each element should be read as
15   1st element is delimiter, 2nd element is enclosure
16   and 3rd element is csv fields
17*/
18$csv_lists = array (
19  array(',', '"', '"water",fruit'),
20  array(',', '"', '"water","fruit"'),
21  array(' ', '^', '^water^ ^fruit^'),
22  array(':', '&', '&water&:&fruit&'),
23  array('=', '=', '=water===fruit='),
24  array('-', '-', '-water--fruit-air'),
25  array('-', '-', '-water---fruit---air-'),
26  array(':', '&', '&""""&:&"&:,:":&,&:,,,,')
27);
28
29$filename = dirname(__FILE__) . '/fgetcsv_variation26.tmp';
30@unlink($filename);
31
32$file_modes = array ("w", "wb", "wt",
33                     "a", "ab", "at",
34                     "x", "xb", "xt");
35
36$loop_counter = 1;
37foreach ($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    $file_handle = fopen($filename, $file_modes[$mode_counter] );
41
42    if ( !$file_handle ) {
43      echo "Error: failed to create file $filename!\n";
44      exit();
45    }
46    $delimiter = $csv_list[0];
47    $enclosure = $csv_list[1];
48    $csv_field = $csv_list[2];
49
50    fwrite($file_handle, $csv_field . "\n");
51    // write another line of text and a blank line
52    // this will be used to test, if the fgetcsv() read more than a line and its
53    // working when only a blank line is read
54    fwrite($file_handle, "This is line of text without csv fields\n");
55    fwrite($file_handle, "\n"); // blank line
56
57    // rewind the file pointer to bof
58      rewind($file_handle);
59
60    echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
61
62    // call fgetcsv() to parse csv fields
63
64    // use the right delimiter and enclosure with max length
65    var_dump( fgetcsv($file_handle, 1024, $delimiter, $enclosure) );
66    // check the file pointer position and if eof
67    var_dump( ftell($file_handle) );
68    var_dump( feof($file_handle) );
69
70    // close the file
71    fclose($file_handle);
72    //delete file
73    unlink($filename);
74  } //end of mode loop
75} // end of foreach
76
77echo "Done\n";
78?>
79--EXPECT--
80*** Testing fgetcsv() : reading the files opened in write only mode ***
81
82-- Testing fgetcsv() with file opened using w mode --
83bool(false)
84int(0)
85bool(false)
86
87-- Testing fgetcsv() with file opened using wb mode --
88bool(false)
89int(0)
90bool(false)
91
92-- Testing fgetcsv() with file opened using wt mode --
93bool(false)
94int(0)
95bool(false)
96
97-- Testing fgetcsv() with file opened using a mode --
98bool(false)
99int(0)
100bool(false)
101
102-- Testing fgetcsv() with file opened using ab mode --
103bool(false)
104int(0)
105bool(false)
106
107-- Testing fgetcsv() with file opened using at mode --
108bool(false)
109int(0)
110bool(false)
111
112-- Testing fgetcsv() with file opened using x mode --
113bool(false)
114int(0)
115bool(false)
116
117-- Testing fgetcsv() with file opened using xb mode --
118bool(false)
119int(0)
120bool(false)
121
122-- Testing fgetcsv() with file opened using xt mode --
123bool(false)
124int(0)
125bool(false)
126
127-- Testing fgetcsv() with file opened using w mode --
128bool(false)
129int(0)
130bool(false)
131
132-- Testing fgetcsv() with file opened using wb mode --
133bool(false)
134int(0)
135bool(false)
136
137-- Testing fgetcsv() with file opened using wt mode --
138bool(false)
139int(0)
140bool(false)
141
142-- Testing fgetcsv() with file opened using a mode --
143bool(false)
144int(0)
145bool(false)
146
147-- Testing fgetcsv() with file opened using ab mode --
148bool(false)
149int(0)
150bool(false)
151
152-- Testing fgetcsv() with file opened using at mode --
153bool(false)
154int(0)
155bool(false)
156
157-- Testing fgetcsv() with file opened using x mode --
158bool(false)
159int(0)
160bool(false)
161
162-- Testing fgetcsv() with file opened using xb mode --
163bool(false)
164int(0)
165bool(false)
166
167-- Testing fgetcsv() with file opened using xt mode --
168bool(false)
169int(0)
170bool(false)
171
172-- Testing fgetcsv() with file opened using w mode --
173bool(false)
174int(0)
175bool(false)
176
177-- Testing fgetcsv() with file opened using wb mode --
178bool(false)
179int(0)
180bool(false)
181
182-- Testing fgetcsv() with file opened using wt mode --
183bool(false)
184int(0)
185bool(false)
186
187-- Testing fgetcsv() with file opened using a mode --
188bool(false)
189int(0)
190bool(false)
191
192-- Testing fgetcsv() with file opened using ab mode --
193bool(false)
194int(0)
195bool(false)
196
197-- Testing fgetcsv() with file opened using at mode --
198bool(false)
199int(0)
200bool(false)
201
202-- Testing fgetcsv() with file opened using x mode --
203bool(false)
204int(0)
205bool(false)
206
207-- Testing fgetcsv() with file opened using xb mode --
208bool(false)
209int(0)
210bool(false)
211
212-- Testing fgetcsv() with file opened using xt mode --
213bool(false)
214int(0)
215bool(false)
216
217-- Testing fgetcsv() with file opened using w mode --
218bool(false)
219int(0)
220bool(false)
221
222-- Testing fgetcsv() with file opened using wb mode --
223bool(false)
224int(0)
225bool(false)
226
227-- Testing fgetcsv() with file opened using wt mode --
228bool(false)
229int(0)
230bool(false)
231
232-- Testing fgetcsv() with file opened using a mode --
233bool(false)
234int(0)
235bool(false)
236
237-- Testing fgetcsv() with file opened using ab mode --
238bool(false)
239int(0)
240bool(false)
241
242-- Testing fgetcsv() with file opened using at mode --
243bool(false)
244int(0)
245bool(false)
246
247-- Testing fgetcsv() with file opened using x mode --
248bool(false)
249int(0)
250bool(false)
251
252-- Testing fgetcsv() with file opened using xb mode --
253bool(false)
254int(0)
255bool(false)
256
257-- Testing fgetcsv() with file opened using xt mode --
258bool(false)
259int(0)
260bool(false)
261
262-- Testing fgetcsv() with file opened using w mode --
263bool(false)
264int(0)
265bool(false)
266
267-- Testing fgetcsv() with file opened using wb mode --
268bool(false)
269int(0)
270bool(false)
271
272-- Testing fgetcsv() with file opened using wt mode --
273bool(false)
274int(0)
275bool(false)
276
277-- Testing fgetcsv() with file opened using a mode --
278bool(false)
279int(0)
280bool(false)
281
282-- Testing fgetcsv() with file opened using ab mode --
283bool(false)
284int(0)
285bool(false)
286
287-- Testing fgetcsv() with file opened using at mode --
288bool(false)
289int(0)
290bool(false)
291
292-- Testing fgetcsv() with file opened using x mode --
293bool(false)
294int(0)
295bool(false)
296
297-- Testing fgetcsv() with file opened using xb mode --
298bool(false)
299int(0)
300bool(false)
301
302-- Testing fgetcsv() with file opened using xt mode --
303bool(false)
304int(0)
305bool(false)
306
307-- Testing fgetcsv() with file opened using w mode --
308bool(false)
309int(0)
310bool(false)
311
312-- Testing fgetcsv() with file opened using wb mode --
313bool(false)
314int(0)
315bool(false)
316
317-- Testing fgetcsv() with file opened using wt mode --
318bool(false)
319int(0)
320bool(false)
321
322-- Testing fgetcsv() with file opened using a mode --
323bool(false)
324int(0)
325bool(false)
326
327-- Testing fgetcsv() with file opened using ab mode --
328bool(false)
329int(0)
330bool(false)
331
332-- Testing fgetcsv() with file opened using at mode --
333bool(false)
334int(0)
335bool(false)
336
337-- Testing fgetcsv() with file opened using x mode --
338bool(false)
339int(0)
340bool(false)
341
342-- Testing fgetcsv() with file opened using xb mode --
343bool(false)
344int(0)
345bool(false)
346
347-- Testing fgetcsv() with file opened using xt mode --
348bool(false)
349int(0)
350bool(false)
351
352-- Testing fgetcsv() with file opened using w mode --
353bool(false)
354int(0)
355bool(false)
356
357-- Testing fgetcsv() with file opened using wb mode --
358bool(false)
359int(0)
360bool(false)
361
362-- Testing fgetcsv() with file opened using wt mode --
363bool(false)
364int(0)
365bool(false)
366
367-- Testing fgetcsv() with file opened using a mode --
368bool(false)
369int(0)
370bool(false)
371
372-- Testing fgetcsv() with file opened using ab mode --
373bool(false)
374int(0)
375bool(false)
376
377-- Testing fgetcsv() with file opened using at mode --
378bool(false)
379int(0)
380bool(false)
381
382-- Testing fgetcsv() with file opened using x mode --
383bool(false)
384int(0)
385bool(false)
386
387-- Testing fgetcsv() with file opened using xb mode --
388bool(false)
389int(0)
390bool(false)
391
392-- Testing fgetcsv() with file opened using xt mode --
393bool(false)
394int(0)
395bool(false)
396
397-- Testing fgetcsv() with file opened using w mode --
398bool(false)
399int(0)
400bool(false)
401
402-- Testing fgetcsv() with file opened using wb mode --
403bool(false)
404int(0)
405bool(false)
406
407-- Testing fgetcsv() with file opened using wt mode --
408bool(false)
409int(0)
410bool(false)
411
412-- Testing fgetcsv() with file opened using a mode --
413bool(false)
414int(0)
415bool(false)
416
417-- Testing fgetcsv() with file opened using ab mode --
418bool(false)
419int(0)
420bool(false)
421
422-- Testing fgetcsv() with file opened using at mode --
423bool(false)
424int(0)
425bool(false)
426
427-- Testing fgetcsv() with file opened using x mode --
428bool(false)
429int(0)
430bool(false)
431
432-- Testing fgetcsv() with file opened using xb mode --
433bool(false)
434int(0)
435bool(false)
436
437-- Testing fgetcsv() with file opened using xt mode --
438bool(false)
439int(0)
440bool(false)
441Done
442