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