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