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