1--TEST--
2Test fgetcsv() : usage variations - with default enclosure
3--FILE--
4<?php
5/* Testing fgetcsv() to read a file when provided with default enclosure character */
6
7echo "*** Testing fgetcsv() : with default enclosure ***\n";
8
9/* the array is with two elements in it. Each element should be read as
10   1st element is delimiter & 2nd element is csv fields
11*/
12$csv_lists = array (
13  array(',', 'water,fruit'),
14  array(' ', 'water fruit'),
15  array(' ', '"water" "fruit"'),
16  array('\\', 'water\\"fruit"\\"air"'),
17  array('\\', '"water"\\"fruit"\\"""'),
18);
19
20$filename = __DIR__ . '/fgetcsv_variation15.tmp';
21@unlink($filename);
22
23$file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
24                     "a+", "a+b", "a+t",
25                     "w+", "w+b", "w+t",
26                     "x+", "x+b", "x+t");
27
28$loop_counter = 1;
29foreach ($csv_lists as $csv_list) {
30  for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
31    // create the file and add the content with has csv fields
32    if ( strstr($file_modes[$mode_counter], "r") ) {
33      $file_handle = fopen($filename, "w");
34    } else {
35      $file_handle = fopen($filename, $file_modes[$mode_counter] );
36    }
37    if ( !$file_handle ) {
38      echo "Error: failed to create file $filename!\n";
39      exit();
40    }
41    $delimiter = $csv_list[0];
42    $csv_field = $csv_list[1];
43    fwrite($file_handle, $csv_field . "\n");
44    // write another line of text and a blank line
45    // this will be used to test, if the fgetcsv() read more than a line and its
46    // working when only a blan line is read
47    fwrite($file_handle, "This is line of text without csv fields\n");
48    fwrite($file_handle, "\n"); // blank line
49
50    // close the file if the mode to be used is read mode  and re-open using read mode
51    // else rewind the file pointer to beginning of the file
52    if ( strstr($file_modes[$mode_counter], "r" ) ) {
53      fclose($file_handle);
54      $file_handle = fopen($filename, $file_modes[$mode_counter]);
55    } else {
56      // rewind the file pointer to bof
57      rewind($file_handle);
58    }
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    var_dump( fgetcsv($file_handle, 1024, $delimiter) );
65    // check the file pointer position and if eof
66    var_dump( ftell($file_handle) );
67    var_dump( feof($file_handle) );
68
69    // close the file
70    fclose($file_handle);
71    //delete file
72    unlink($filename);
73  } //end of mode loop
74} // end of foreach
75
76echo "Done\n";
77?>
78--EXPECT--
79*** Testing fgetcsv() : with default enclosure ***
80
81-- Testing fgetcsv() with file opened using r mode --
82array(2) {
83  [0]=>
84  string(5) "water"
85  [1]=>
86  string(5) "fruit"
87}
88int(12)
89bool(false)
90
91-- Testing fgetcsv() with file opened using rb mode --
92array(2) {
93  [0]=>
94  string(5) "water"
95  [1]=>
96  string(5) "fruit"
97}
98int(12)
99bool(false)
100
101-- Testing fgetcsv() with file opened using rt mode --
102array(2) {
103  [0]=>
104  string(5) "water"
105  [1]=>
106  string(5) "fruit"
107}
108int(12)
109bool(false)
110
111-- Testing fgetcsv() with file opened using r+ mode --
112array(2) {
113  [0]=>
114  string(5) "water"
115  [1]=>
116  string(5) "fruit"
117}
118int(12)
119bool(false)
120
121-- Testing fgetcsv() with file opened using r+b mode --
122array(2) {
123  [0]=>
124  string(5) "water"
125  [1]=>
126  string(5) "fruit"
127}
128int(12)
129bool(false)
130
131-- Testing fgetcsv() with file opened using r+t mode --
132array(2) {
133  [0]=>
134  string(5) "water"
135  [1]=>
136  string(5) "fruit"
137}
138int(12)
139bool(false)
140
141-- Testing fgetcsv() with file opened using a+ mode --
142array(2) {
143  [0]=>
144  string(5) "water"
145  [1]=>
146  string(5) "fruit"
147}
148int(12)
149bool(false)
150
151-- Testing fgetcsv() with file opened using a+b mode --
152array(2) {
153  [0]=>
154  string(5) "water"
155  [1]=>
156  string(5) "fruit"
157}
158int(12)
159bool(false)
160
161-- Testing fgetcsv() with file opened using a+t mode --
162array(2) {
163  [0]=>
164  string(5) "water"
165  [1]=>
166  string(5) "fruit"
167}
168int(12)
169bool(false)
170
171-- Testing fgetcsv() with file opened using w+ mode --
172array(2) {
173  [0]=>
174  string(5) "water"
175  [1]=>
176  string(5) "fruit"
177}
178int(12)
179bool(false)
180
181-- Testing fgetcsv() with file opened using w+b mode --
182array(2) {
183  [0]=>
184  string(5) "water"
185  [1]=>
186  string(5) "fruit"
187}
188int(12)
189bool(false)
190
191-- Testing fgetcsv() with file opened using w+t mode --
192array(2) {
193  [0]=>
194  string(5) "water"
195  [1]=>
196  string(5) "fruit"
197}
198int(12)
199bool(false)
200
201-- Testing fgetcsv() with file opened using x+ mode --
202array(2) {
203  [0]=>
204  string(5) "water"
205  [1]=>
206  string(5) "fruit"
207}
208int(12)
209bool(false)
210
211-- Testing fgetcsv() with file opened using x+b mode --
212array(2) {
213  [0]=>
214  string(5) "water"
215  [1]=>
216  string(5) "fruit"
217}
218int(12)
219bool(false)
220
221-- Testing fgetcsv() with file opened using x+t mode --
222array(2) {
223  [0]=>
224  string(5) "water"
225  [1]=>
226  string(5) "fruit"
227}
228int(12)
229bool(false)
230
231-- Testing fgetcsv() with file opened using r mode --
232array(2) {
233  [0]=>
234  string(5) "water"
235  [1]=>
236  string(5) "fruit"
237}
238int(12)
239bool(false)
240
241-- Testing fgetcsv() with file opened using rb mode --
242array(2) {
243  [0]=>
244  string(5) "water"
245  [1]=>
246  string(5) "fruit"
247}
248int(12)
249bool(false)
250
251-- Testing fgetcsv() with file opened using rt mode --
252array(2) {
253  [0]=>
254  string(5) "water"
255  [1]=>
256  string(5) "fruit"
257}
258int(12)
259bool(false)
260
261-- Testing fgetcsv() with file opened using r+ mode --
262array(2) {
263  [0]=>
264  string(5) "water"
265  [1]=>
266  string(5) "fruit"
267}
268int(12)
269bool(false)
270
271-- Testing fgetcsv() with file opened using r+b mode --
272array(2) {
273  [0]=>
274  string(5) "water"
275  [1]=>
276  string(5) "fruit"
277}
278int(12)
279bool(false)
280
281-- Testing fgetcsv() with file opened using r+t mode --
282array(2) {
283  [0]=>
284  string(5) "water"
285  [1]=>
286  string(5) "fruit"
287}
288int(12)
289bool(false)
290
291-- Testing fgetcsv() with file opened using a+ mode --
292array(2) {
293  [0]=>
294  string(5) "water"
295  [1]=>
296  string(5) "fruit"
297}
298int(12)
299bool(false)
300
301-- Testing fgetcsv() with file opened using a+b mode --
302array(2) {
303  [0]=>
304  string(5) "water"
305  [1]=>
306  string(5) "fruit"
307}
308int(12)
309bool(false)
310
311-- Testing fgetcsv() with file opened using a+t mode --
312array(2) {
313  [0]=>
314  string(5) "water"
315  [1]=>
316  string(5) "fruit"
317}
318int(12)
319bool(false)
320
321-- Testing fgetcsv() with file opened using w+ mode --
322array(2) {
323  [0]=>
324  string(5) "water"
325  [1]=>
326  string(5) "fruit"
327}
328int(12)
329bool(false)
330
331-- Testing fgetcsv() with file opened using w+b mode --
332array(2) {
333  [0]=>
334  string(5) "water"
335  [1]=>
336  string(5) "fruit"
337}
338int(12)
339bool(false)
340
341-- Testing fgetcsv() with file opened using w+t mode --
342array(2) {
343  [0]=>
344  string(5) "water"
345  [1]=>
346  string(5) "fruit"
347}
348int(12)
349bool(false)
350
351-- Testing fgetcsv() with file opened using x+ mode --
352array(2) {
353  [0]=>
354  string(5) "water"
355  [1]=>
356  string(5) "fruit"
357}
358int(12)
359bool(false)
360
361-- Testing fgetcsv() with file opened using x+b mode --
362array(2) {
363  [0]=>
364  string(5) "water"
365  [1]=>
366  string(5) "fruit"
367}
368int(12)
369bool(false)
370
371-- Testing fgetcsv() with file opened using x+t mode --
372array(2) {
373  [0]=>
374  string(5) "water"
375  [1]=>
376  string(5) "fruit"
377}
378int(12)
379bool(false)
380
381-- Testing fgetcsv() with file opened using r mode --
382array(2) {
383  [0]=>
384  string(5) "water"
385  [1]=>
386  string(5) "fruit"
387}
388int(16)
389bool(false)
390
391-- Testing fgetcsv() with file opened using rb mode --
392array(2) {
393  [0]=>
394  string(5) "water"
395  [1]=>
396  string(5) "fruit"
397}
398int(16)
399bool(false)
400
401-- Testing fgetcsv() with file opened using rt mode --
402array(2) {
403  [0]=>
404  string(5) "water"
405  [1]=>
406  string(5) "fruit"
407}
408int(16)
409bool(false)
410
411-- Testing fgetcsv() with file opened using r+ mode --
412array(2) {
413  [0]=>
414  string(5) "water"
415  [1]=>
416  string(5) "fruit"
417}
418int(16)
419bool(false)
420
421-- Testing fgetcsv() with file opened using r+b mode --
422array(2) {
423  [0]=>
424  string(5) "water"
425  [1]=>
426  string(5) "fruit"
427}
428int(16)
429bool(false)
430
431-- Testing fgetcsv() with file opened using r+t mode --
432array(2) {
433  [0]=>
434  string(5) "water"
435  [1]=>
436  string(5) "fruit"
437}
438int(16)
439bool(false)
440
441-- Testing fgetcsv() with file opened using a+ mode --
442array(2) {
443  [0]=>
444  string(5) "water"
445  [1]=>
446  string(5) "fruit"
447}
448int(16)
449bool(false)
450
451-- Testing fgetcsv() with file opened using a+b mode --
452array(2) {
453  [0]=>
454  string(5) "water"
455  [1]=>
456  string(5) "fruit"
457}
458int(16)
459bool(false)
460
461-- Testing fgetcsv() with file opened using a+t mode --
462array(2) {
463  [0]=>
464  string(5) "water"
465  [1]=>
466  string(5) "fruit"
467}
468int(16)
469bool(false)
470
471-- Testing fgetcsv() with file opened using w+ mode --
472array(2) {
473  [0]=>
474  string(5) "water"
475  [1]=>
476  string(5) "fruit"
477}
478int(16)
479bool(false)
480
481-- Testing fgetcsv() with file opened using w+b mode --
482array(2) {
483  [0]=>
484  string(5) "water"
485  [1]=>
486  string(5) "fruit"
487}
488int(16)
489bool(false)
490
491-- Testing fgetcsv() with file opened using w+t mode --
492array(2) {
493  [0]=>
494  string(5) "water"
495  [1]=>
496  string(5) "fruit"
497}
498int(16)
499bool(false)
500
501-- Testing fgetcsv() with file opened using x+ mode --
502array(2) {
503  [0]=>
504  string(5) "water"
505  [1]=>
506  string(5) "fruit"
507}
508int(16)
509bool(false)
510
511-- Testing fgetcsv() with file opened using x+b mode --
512array(2) {
513  [0]=>
514  string(5) "water"
515  [1]=>
516  string(5) "fruit"
517}
518int(16)
519bool(false)
520
521-- Testing fgetcsv() with file opened using x+t mode --
522array(2) {
523  [0]=>
524  string(5) "water"
525  [1]=>
526  string(5) "fruit"
527}
528int(16)
529bool(false)
530
531-- Testing fgetcsv() with file opened using r mode --
532array(3) {
533  [0]=>
534  string(5) "water"
535  [1]=>
536  string(5) "fruit"
537  [2]=>
538  string(3) "air"
539}
540int(20)
541bool(false)
542
543-- Testing fgetcsv() with file opened using rb mode --
544array(3) {
545  [0]=>
546  string(5) "water"
547  [1]=>
548  string(5) "fruit"
549  [2]=>
550  string(3) "air"
551}
552int(20)
553bool(false)
554
555-- Testing fgetcsv() with file opened using rt mode --
556array(3) {
557  [0]=>
558  string(5) "water"
559  [1]=>
560  string(5) "fruit"
561  [2]=>
562  string(3) "air"
563}
564int(20)
565bool(false)
566
567-- Testing fgetcsv() with file opened using r+ mode --
568array(3) {
569  [0]=>
570  string(5) "water"
571  [1]=>
572  string(5) "fruit"
573  [2]=>
574  string(3) "air"
575}
576int(20)
577bool(false)
578
579-- Testing fgetcsv() with file opened using r+b mode --
580array(3) {
581  [0]=>
582  string(5) "water"
583  [1]=>
584  string(5) "fruit"
585  [2]=>
586  string(3) "air"
587}
588int(20)
589bool(false)
590
591-- Testing fgetcsv() with file opened using r+t mode --
592array(3) {
593  [0]=>
594  string(5) "water"
595  [1]=>
596  string(5) "fruit"
597  [2]=>
598  string(3) "air"
599}
600int(20)
601bool(false)
602
603-- Testing fgetcsv() with file opened using a+ mode --
604array(3) {
605  [0]=>
606  string(5) "water"
607  [1]=>
608  string(5) "fruit"
609  [2]=>
610  string(3) "air"
611}
612int(20)
613bool(false)
614
615-- Testing fgetcsv() with file opened using a+b mode --
616array(3) {
617  [0]=>
618  string(5) "water"
619  [1]=>
620  string(5) "fruit"
621  [2]=>
622  string(3) "air"
623}
624int(20)
625bool(false)
626
627-- Testing fgetcsv() with file opened using a+t mode --
628array(3) {
629  [0]=>
630  string(5) "water"
631  [1]=>
632  string(5) "fruit"
633  [2]=>
634  string(3) "air"
635}
636int(20)
637bool(false)
638
639-- Testing fgetcsv() with file opened using w+ mode --
640array(3) {
641  [0]=>
642  string(5) "water"
643  [1]=>
644  string(5) "fruit"
645  [2]=>
646  string(3) "air"
647}
648int(20)
649bool(false)
650
651-- Testing fgetcsv() with file opened using w+b mode --
652array(3) {
653  [0]=>
654  string(5) "water"
655  [1]=>
656  string(5) "fruit"
657  [2]=>
658  string(3) "air"
659}
660int(20)
661bool(false)
662
663-- Testing fgetcsv() with file opened using w+t mode --
664array(3) {
665  [0]=>
666  string(5) "water"
667  [1]=>
668  string(5) "fruit"
669  [2]=>
670  string(3) "air"
671}
672int(20)
673bool(false)
674
675-- Testing fgetcsv() with file opened using x+ mode --
676array(3) {
677  [0]=>
678  string(5) "water"
679  [1]=>
680  string(5) "fruit"
681  [2]=>
682  string(3) "air"
683}
684int(20)
685bool(false)
686
687-- Testing fgetcsv() with file opened using x+b mode --
688array(3) {
689  [0]=>
690  string(5) "water"
691  [1]=>
692  string(5) "fruit"
693  [2]=>
694  string(3) "air"
695}
696int(20)
697bool(false)
698
699-- Testing fgetcsv() with file opened using x+t mode --
700array(3) {
701  [0]=>
702  string(5) "water"
703  [1]=>
704  string(5) "fruit"
705  [2]=>
706  string(3) "air"
707}
708int(20)
709bool(false)
710
711-- Testing fgetcsv() with file opened using r mode --
712array(3) {
713  [0]=>
714  string(5) "water"
715  [1]=>
716  string(5) "fruit"
717  [2]=>
718  string(43) ""
719This is line of text without csv fields
720
721"
722}
723int(61)
724bool(true)
725
726-- Testing fgetcsv() with file opened using rb mode --
727array(3) {
728  [0]=>
729  string(5) "water"
730  [1]=>
731  string(5) "fruit"
732  [2]=>
733  string(43) ""
734This is line of text without csv fields
735
736"
737}
738int(61)
739bool(true)
740
741-- Testing fgetcsv() with file opened using rt mode --
742array(3) {
743  [0]=>
744  string(5) "water"
745  [1]=>
746  string(5) "fruit"
747  [2]=>
748  string(43) ""
749This is line of text without csv fields
750
751"
752}
753int(61)
754bool(true)
755
756-- Testing fgetcsv() with file opened using r+ mode --
757array(3) {
758  [0]=>
759  string(5) "water"
760  [1]=>
761  string(5) "fruit"
762  [2]=>
763  string(43) ""
764This is line of text without csv fields
765
766"
767}
768int(61)
769bool(true)
770
771-- Testing fgetcsv() with file opened using r+b mode --
772array(3) {
773  [0]=>
774  string(5) "water"
775  [1]=>
776  string(5) "fruit"
777  [2]=>
778  string(43) ""
779This is line of text without csv fields
780
781"
782}
783int(61)
784bool(true)
785
786-- Testing fgetcsv() with file opened using r+t mode --
787array(3) {
788  [0]=>
789  string(5) "water"
790  [1]=>
791  string(5) "fruit"
792  [2]=>
793  string(43) ""
794This is line of text without csv fields
795
796"
797}
798int(61)
799bool(true)
800
801-- Testing fgetcsv() with file opened using a+ mode --
802array(3) {
803  [0]=>
804  string(5) "water"
805  [1]=>
806  string(5) "fruit"
807  [2]=>
808  string(43) ""
809This is line of text without csv fields
810
811"
812}
813int(61)
814bool(true)
815
816-- Testing fgetcsv() with file opened using a+b mode --
817array(3) {
818  [0]=>
819  string(5) "water"
820  [1]=>
821  string(5) "fruit"
822  [2]=>
823  string(43) ""
824This is line of text without csv fields
825
826"
827}
828int(61)
829bool(true)
830
831-- Testing fgetcsv() with file opened using a+t mode --
832array(3) {
833  [0]=>
834  string(5) "water"
835  [1]=>
836  string(5) "fruit"
837  [2]=>
838  string(43) ""
839This is line of text without csv fields
840
841"
842}
843int(61)
844bool(true)
845
846-- Testing fgetcsv() with file opened using w+ mode --
847array(3) {
848  [0]=>
849  string(5) "water"
850  [1]=>
851  string(5) "fruit"
852  [2]=>
853  string(43) ""
854This is line of text without csv fields
855
856"
857}
858int(61)
859bool(true)
860
861-- Testing fgetcsv() with file opened using w+b mode --
862array(3) {
863  [0]=>
864  string(5) "water"
865  [1]=>
866  string(5) "fruit"
867  [2]=>
868  string(43) ""
869This is line of text without csv fields
870
871"
872}
873int(61)
874bool(true)
875
876-- Testing fgetcsv() with file opened using w+t mode --
877array(3) {
878  [0]=>
879  string(5) "water"
880  [1]=>
881  string(5) "fruit"
882  [2]=>
883  string(43) ""
884This is line of text without csv fields
885
886"
887}
888int(61)
889bool(true)
890
891-- Testing fgetcsv() with file opened using x+ mode --
892array(3) {
893  [0]=>
894  string(5) "water"
895  [1]=>
896  string(5) "fruit"
897  [2]=>
898  string(43) ""
899This is line of text without csv fields
900
901"
902}
903int(61)
904bool(true)
905
906-- Testing fgetcsv() with file opened using x+b mode --
907array(3) {
908  [0]=>
909  string(5) "water"
910  [1]=>
911  string(5) "fruit"
912  [2]=>
913  string(43) ""
914This is line of text without csv fields
915
916"
917}
918int(61)
919bool(true)
920
921-- Testing fgetcsv() with file opened using x+t mode --
922array(3) {
923  [0]=>
924  string(5) "water"
925  [1]=>
926  string(5) "fruit"
927  [2]=>
928  string(43) ""
929This is line of text without csv fields
930
931"
932}
933int(61)
934bool(true)
935Done
936