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