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