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