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