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