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