1--TEST--
2Test fgetcsv() : usage variations - with default enclosure, file pointer pointing at end of file
3--FILE--
4<?php
5/*
6   Testing fgetcsv() to read a file whose file pointer is pointing to end of file
7   and fgetcsv() provided with default enclosure argument
8*/
9
10echo "*** Testing fgetcsv() : with default enclosure, file pointer pointing at end of file ***\n";
11
12/* the array is with two elements in it. Each element should be read as
13   1st element is delimiter & 2nd element is csv fields
14*/
15$csv_lists = array (
16  array(',', 'water,fruit'),
17  array(' ', 'water fruit'),
18  array(' ', '"water" "fruit"'),
19  array('\\', 'water\\"fruit"\\"air"'),
20  array('\\', '"water"\\"fruit"\\"""'),
21);
22
23$filename = __DIR__ . '/fgetcsv_variation22.tmp';
24@unlink($filename);
25
26$file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
27                     "a+", "a+b", "a+t",
28                     "w+", "w+b", "w+t",
29                     "x+", "x+b", "x+t");
30
31$loop_counter = 1;
32foreach ($csv_lists as $csv_list) {
33  for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
34    // create the file and add the content with has csv fields
35    if ( strstr($file_modes[$mode_counter], "r") ) {
36      $file_handle = fopen($filename, "w");
37    } else {
38      $file_handle = fopen($filename, $file_modes[$mode_counter] );
39    }
40    if ( !$file_handle ) {
41      echo "Error: failed to create file $filename!\n";
42      exit();
43    }
44    $delimiter = $csv_list[0];
45    $csv_field = $csv_list[1];
46
47    fwrite($file_handle, $csv_field . "\n");
48    // write another line of text and a blank line
49    // this will be used to test, if the fgetcsv() read more than a line and its
50    // working when only a blan line is read
51    fwrite($file_handle, "This is line of text without csv fields\n");
52    fwrite($file_handle, "\n"); // blank line
53
54    // close the file if the mode to be used is read mode  and re-open using read mode
55    if ( strstr($file_modes[$mode_counter], "r" ) ) {
56      fclose($file_handle);
57      $file_handle = fopen($filename, $file_modes[$mode_counter]);
58    }
59
60    echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
61
62    // set the file pointer to EOF
63    var_dump( fseek($file_handle, 0, SEEK_END) );
64
65    // call fgetcsv() to parse csv fields
66
67    // now file pointer should point to end of the file, try reading again
68    var_dump( feof($file_handle) );
69    var_dump( fgetcsv($file_handle, 1024, $delimiter) ); // with length, delimiter
70    // check the file pointer position and if eof
71    var_dump( ftell($file_handle) );
72    var_dump( feof($file_handle) );
73    // close the file
74    fclose($file_handle);
75    //delete file
76    unlink($filename);
77  } //end of mode loop
78} // end of foreach
79
80echo "Done\n";
81?>
82--EXPECTF--
83*** Testing fgetcsv() : with default enclosure, file pointer pointing at end of file ***
84
85-- Testing fgetcsv() with file opened using r mode --
86int(0)
87bool(false)
88bool(false)
89int(53)
90bool(true)
91
92-- Testing fgetcsv() with file opened using rb mode --
93int(0)
94bool(false)
95bool(false)
96int(53)
97bool(true)
98
99-- Testing fgetcsv() with file opened using rt mode --
100int(0)
101bool(false)
102bool(false)
103int(%d)
104bool(true)
105
106-- Testing fgetcsv() with file opened using r+ mode --
107int(0)
108bool(false)
109bool(false)
110int(53)
111bool(true)
112
113-- Testing fgetcsv() with file opened using r+b mode --
114int(0)
115bool(false)
116bool(false)
117int(53)
118bool(true)
119
120-- Testing fgetcsv() with file opened using r+t mode --
121int(0)
122bool(false)
123bool(false)
124int(%d)
125bool(true)
126
127-- Testing fgetcsv() with file opened using a+ mode --
128int(0)
129bool(false)
130bool(false)
131int(53)
132bool(true)
133
134-- Testing fgetcsv() with file opened using a+b mode --
135int(0)
136bool(false)
137bool(false)
138int(53)
139bool(true)
140
141-- Testing fgetcsv() with file opened using a+t mode --
142int(0)
143bool(false)
144bool(false)
145int(%d)
146bool(true)
147
148-- Testing fgetcsv() with file opened using w+ mode --
149int(0)
150bool(false)
151bool(false)
152int(53)
153bool(true)
154
155-- Testing fgetcsv() with file opened using w+b mode --
156int(0)
157bool(false)
158bool(false)
159int(53)
160bool(true)
161
162-- Testing fgetcsv() with file opened using w+t mode --
163int(0)
164bool(false)
165bool(false)
166int(%d)
167bool(true)
168
169-- Testing fgetcsv() with file opened using x+ mode --
170int(0)
171bool(false)
172bool(false)
173int(53)
174bool(true)
175
176-- Testing fgetcsv() with file opened using x+b mode --
177int(0)
178bool(false)
179bool(false)
180int(53)
181bool(true)
182
183-- Testing fgetcsv() with file opened using x+t mode --
184int(0)
185bool(false)
186bool(false)
187int(%d)
188bool(true)
189
190-- Testing fgetcsv() with file opened using r mode --
191int(0)
192bool(false)
193bool(false)
194int(53)
195bool(true)
196
197-- Testing fgetcsv() with file opened using rb mode --
198int(0)
199bool(false)
200bool(false)
201int(53)
202bool(true)
203
204-- Testing fgetcsv() with file opened using rt mode --
205int(0)
206bool(false)
207bool(false)
208int(%d)
209bool(true)
210
211-- Testing fgetcsv() with file opened using r+ mode --
212int(0)
213bool(false)
214bool(false)
215int(53)
216bool(true)
217
218-- Testing fgetcsv() with file opened using r+b mode --
219int(0)
220bool(false)
221bool(false)
222int(53)
223bool(true)
224
225-- Testing fgetcsv() with file opened using r+t mode --
226int(0)
227bool(false)
228bool(false)
229int(%d)
230bool(true)
231
232-- Testing fgetcsv() with file opened using a+ mode --
233int(0)
234bool(false)
235bool(false)
236int(53)
237bool(true)
238
239-- Testing fgetcsv() with file opened using a+b mode --
240int(0)
241bool(false)
242bool(false)
243int(53)
244bool(true)
245
246-- Testing fgetcsv() with file opened using a+t mode --
247int(0)
248bool(false)
249bool(false)
250int(%d)
251bool(true)
252
253-- Testing fgetcsv() with file opened using w+ mode --
254int(0)
255bool(false)
256bool(false)
257int(53)
258bool(true)
259
260-- Testing fgetcsv() with file opened using w+b mode --
261int(0)
262bool(false)
263bool(false)
264int(53)
265bool(true)
266
267-- Testing fgetcsv() with file opened using w+t mode --
268int(0)
269bool(false)
270bool(false)
271int(%d)
272bool(true)
273
274-- Testing fgetcsv() with file opened using x+ mode --
275int(0)
276bool(false)
277bool(false)
278int(53)
279bool(true)
280
281-- Testing fgetcsv() with file opened using x+b mode --
282int(0)
283bool(false)
284bool(false)
285int(53)
286bool(true)
287
288-- Testing fgetcsv() with file opened using x+t mode --
289int(0)
290bool(false)
291bool(false)
292int(%d)
293bool(true)
294
295-- Testing fgetcsv() with file opened using r mode --
296int(0)
297bool(false)
298bool(false)
299int(57)
300bool(true)
301
302-- Testing fgetcsv() with file opened using rb mode --
303int(0)
304bool(false)
305bool(false)
306int(57)
307bool(true)
308
309-- Testing fgetcsv() with file opened using rt mode --
310int(0)
311bool(false)
312bool(false)
313int(%d)
314bool(true)
315
316-- Testing fgetcsv() with file opened using r+ mode --
317int(0)
318bool(false)
319bool(false)
320int(57)
321bool(true)
322
323-- Testing fgetcsv() with file opened using r+b mode --
324int(0)
325bool(false)
326bool(false)
327int(57)
328bool(true)
329
330-- Testing fgetcsv() with file opened using r+t mode --
331int(0)
332bool(false)
333bool(false)
334int(%d)
335bool(true)
336
337-- Testing fgetcsv() with file opened using a+ mode --
338int(0)
339bool(false)
340bool(false)
341int(57)
342bool(true)
343
344-- Testing fgetcsv() with file opened using a+b mode --
345int(0)
346bool(false)
347bool(false)
348int(57)
349bool(true)
350
351-- Testing fgetcsv() with file opened using a+t mode --
352int(0)
353bool(false)
354bool(false)
355int(%d)
356bool(true)
357
358-- Testing fgetcsv() with file opened using w+ mode --
359int(0)
360bool(false)
361bool(false)
362int(57)
363bool(true)
364
365-- Testing fgetcsv() with file opened using w+b mode --
366int(0)
367bool(false)
368bool(false)
369int(57)
370bool(true)
371
372-- Testing fgetcsv() with file opened using w+t mode --
373int(0)
374bool(false)
375bool(false)
376int(%d)
377bool(true)
378
379-- Testing fgetcsv() with file opened using x+ mode --
380int(0)
381bool(false)
382bool(false)
383int(57)
384bool(true)
385
386-- Testing fgetcsv() with file opened using x+b mode --
387int(0)
388bool(false)
389bool(false)
390int(57)
391bool(true)
392
393-- Testing fgetcsv() with file opened using x+t mode --
394int(0)
395bool(false)
396bool(false)
397int(%d)
398bool(true)
399
400-- Testing fgetcsv() with file opened using r mode --
401int(0)
402bool(false)
403bool(false)
404int(61)
405bool(true)
406
407-- Testing fgetcsv() with file opened using rb mode --
408int(0)
409bool(false)
410bool(false)
411int(61)
412bool(true)
413
414-- Testing fgetcsv() with file opened using rt mode --
415int(0)
416bool(false)
417bool(false)
418int(%d)
419bool(true)
420
421-- Testing fgetcsv() with file opened using r+ mode --
422int(0)
423bool(false)
424bool(false)
425int(61)
426bool(true)
427
428-- Testing fgetcsv() with file opened using r+b mode --
429int(0)
430bool(false)
431bool(false)
432int(61)
433bool(true)
434
435-- Testing fgetcsv() with file opened using r+t mode --
436int(0)
437bool(false)
438bool(false)
439int(%d)
440bool(true)
441
442-- Testing fgetcsv() with file opened using a+ mode --
443int(0)
444bool(false)
445bool(false)
446int(61)
447bool(true)
448
449-- Testing fgetcsv() with file opened using a+b mode --
450int(0)
451bool(false)
452bool(false)
453int(61)
454bool(true)
455
456-- Testing fgetcsv() with file opened using a+t mode --
457int(0)
458bool(false)
459bool(false)
460int(%d)
461bool(true)
462
463-- Testing fgetcsv() with file opened using w+ mode --
464int(0)
465bool(false)
466bool(false)
467int(61)
468bool(true)
469
470-- Testing fgetcsv() with file opened using w+b mode --
471int(0)
472bool(false)
473bool(false)
474int(61)
475bool(true)
476
477-- Testing fgetcsv() with file opened using w+t mode --
478int(0)
479bool(false)
480bool(false)
481int(%d)
482bool(true)
483
484-- Testing fgetcsv() with file opened using x+ mode --
485int(0)
486bool(false)
487bool(false)
488int(61)
489bool(true)
490
491-- Testing fgetcsv() with file opened using x+b mode --
492int(0)
493bool(false)
494bool(false)
495int(61)
496bool(true)
497
498-- Testing fgetcsv() with file opened using x+t mode --
499int(0)
500bool(false)
501bool(false)
502int(%d)
503bool(true)
504
505-- Testing fgetcsv() with file opened using r mode --
506int(0)
507bool(false)
508bool(false)
509int(61)
510bool(true)
511
512-- Testing fgetcsv() with file opened using rb mode --
513int(0)
514bool(false)
515bool(false)
516int(61)
517bool(true)
518
519-- Testing fgetcsv() with file opened using rt mode --
520int(0)
521bool(false)
522bool(false)
523int(%d)
524bool(true)
525
526-- Testing fgetcsv() with file opened using r+ mode --
527int(0)
528bool(false)
529bool(false)
530int(61)
531bool(true)
532
533-- Testing fgetcsv() with file opened using r+b mode --
534int(0)
535bool(false)
536bool(false)
537int(61)
538bool(true)
539
540-- Testing fgetcsv() with file opened using r+t mode --
541int(0)
542bool(false)
543bool(false)
544int(%d)
545bool(true)
546
547-- Testing fgetcsv() with file opened using a+ mode --
548int(0)
549bool(false)
550bool(false)
551int(61)
552bool(true)
553
554-- Testing fgetcsv() with file opened using a+b mode --
555int(0)
556bool(false)
557bool(false)
558int(61)
559bool(true)
560
561-- Testing fgetcsv() with file opened using a+t mode --
562int(0)
563bool(false)
564bool(false)
565int(%d)
566bool(true)
567
568-- Testing fgetcsv() with file opened using w+ mode --
569int(0)
570bool(false)
571bool(false)
572int(61)
573bool(true)
574
575-- Testing fgetcsv() with file opened using w+b mode --
576int(0)
577bool(false)
578bool(false)
579int(61)
580bool(true)
581
582-- Testing fgetcsv() with file opened using w+t mode --
583int(0)
584bool(false)
585bool(false)
586int(%d)
587bool(true)
588
589-- Testing fgetcsv() with file opened using x+ mode --
590int(0)
591bool(false)
592bool(false)
593int(61)
594bool(true)
595
596-- Testing fgetcsv() with file opened using x+b mode --
597int(0)
598bool(false)
599bool(false)
600int(61)
601bool(true)
602
603-- Testing fgetcsv() with file opened using x+t mode --
604int(0)
605bool(false)
606bool(false)
607int(%d)
608bool(true)
609Done
610