1--TEST-- 2Test fgetcsv() : usage variations - with file handle and length, file pointer pointing at end of file 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 whose file pointer is pointing to end of file 12 and fgetcsv() provided with file handle and length arguments 13*/ 14 15echo "*** Testing fgetcsv() : with file handle and length arguments, file pointer pointing at end of file ***\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_variation30.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 52 fwrite($file_handle, $csv_field . "\n"); 53 // write another line of text and a blank line 54 // this will be used to test, if the fgetcsv() read more than a line and its 55 // working when only a blan line is read 56 fwrite($file_handle, "This is line of text without csv fields\n"); 57 fwrite($file_handle, "\n"); // blank line 58 59 // close the file if the mode to be used is read mode and re-open using read mode 60 if ( strstr($file_modes[$mode_counter], "r" ) ) { 61 fclose($file_handle); 62 $file_handle = fopen($filename, $file_modes[$mode_counter]); 63 } 64 65 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n"; 66 67 // set the file pointer to EOF 68 var_dump( fseek($file_handle, 0, SEEK_END) ); 69 70 // call fgetcsv() to parse csv fields 71 72 // now file pointer should point to end of the file, try reading again 73 var_dump( feof($file_handle) ); 74 var_dump( fgetcsv($file_handle, 1024) ); 75 // check the file pointer position and if eof 76 var_dump( ftell($file_handle) ); 77 var_dump( feof($file_handle) ); 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--EXPECTF-- 88*** Testing fgetcsv() : with file handle and length arguments, file pointer pointing at end of file *** 89 90-- Testing fgetcsv() with file opened using r mode -- 91int(0) 92bool(false) 93bool(false) 94int(53) 95bool(true) 96 97-- Testing fgetcsv() with file opened using rb mode -- 98int(0) 99bool(false) 100bool(false) 101int(53) 102bool(true) 103 104-- Testing fgetcsv() with file opened using rt mode -- 105int(0) 106bool(false) 107bool(false) 108int(%d) 109bool(true) 110 111-- Testing fgetcsv() with file opened using r+ mode -- 112int(0) 113bool(false) 114bool(false) 115int(53) 116bool(true) 117 118-- Testing fgetcsv() with file opened using r+b mode -- 119int(0) 120bool(false) 121bool(false) 122int(53) 123bool(true) 124 125-- Testing fgetcsv() with file opened using r+t mode -- 126int(0) 127bool(false) 128bool(false) 129int(%d) 130bool(true) 131 132-- Testing fgetcsv() with file opened using a+ mode -- 133int(0) 134bool(false) 135bool(false) 136int(53) 137bool(true) 138 139-- Testing fgetcsv() with file opened using a+b mode -- 140int(0) 141bool(false) 142bool(false) 143int(53) 144bool(true) 145 146-- Testing fgetcsv() with file opened using a+t mode -- 147int(0) 148bool(false) 149bool(false) 150int(%d) 151bool(true) 152 153-- Testing fgetcsv() with file opened using w+ mode -- 154int(0) 155bool(false) 156bool(false) 157int(53) 158bool(true) 159 160-- Testing fgetcsv() with file opened using w+b mode -- 161int(0) 162bool(false) 163bool(false) 164int(53) 165bool(true) 166 167-- Testing fgetcsv() with file opened using w+t mode -- 168int(0) 169bool(false) 170bool(false) 171int(%d) 172bool(true) 173 174-- Testing fgetcsv() with file opened using x+ mode -- 175int(0) 176bool(false) 177bool(false) 178int(53) 179bool(true) 180 181-- Testing fgetcsv() with file opened using x+b mode -- 182int(0) 183bool(false) 184bool(false) 185int(53) 186bool(true) 187 188-- Testing fgetcsv() with file opened using x+t mode -- 189int(0) 190bool(false) 191bool(false) 192int(%d) 193bool(true) 194 195-- Testing fgetcsv() with file opened using r mode -- 196int(0) 197bool(false) 198bool(false) 199int(53) 200bool(true) 201 202-- Testing fgetcsv() with file opened using rb mode -- 203int(0) 204bool(false) 205bool(false) 206int(53) 207bool(true) 208 209-- Testing fgetcsv() with file opened using rt mode -- 210int(0) 211bool(false) 212bool(false) 213int(%d) 214bool(true) 215 216-- Testing fgetcsv() with file opened using r+ mode -- 217int(0) 218bool(false) 219bool(false) 220int(53) 221bool(true) 222 223-- Testing fgetcsv() with file opened using r+b mode -- 224int(0) 225bool(false) 226bool(false) 227int(53) 228bool(true) 229 230-- Testing fgetcsv() with file opened using r+t mode -- 231int(0) 232bool(false) 233bool(false) 234int(%d) 235bool(true) 236 237-- Testing fgetcsv() with file opened using a+ mode -- 238int(0) 239bool(false) 240bool(false) 241int(53) 242bool(true) 243 244-- Testing fgetcsv() with file opened using a+b mode -- 245int(0) 246bool(false) 247bool(false) 248int(53) 249bool(true) 250 251-- Testing fgetcsv() with file opened using a+t mode -- 252int(0) 253bool(false) 254bool(false) 255int(%d) 256bool(true) 257 258-- Testing fgetcsv() with file opened using w+ mode -- 259int(0) 260bool(false) 261bool(false) 262int(53) 263bool(true) 264 265-- Testing fgetcsv() with file opened using w+b mode -- 266int(0) 267bool(false) 268bool(false) 269int(53) 270bool(true) 271 272-- Testing fgetcsv() with file opened using w+t mode -- 273int(0) 274bool(false) 275bool(false) 276int(%d) 277bool(true) 278 279-- Testing fgetcsv() with file opened using x+ mode -- 280int(0) 281bool(false) 282bool(false) 283int(53) 284bool(true) 285 286-- Testing fgetcsv() with file opened using x+b mode -- 287int(0) 288bool(false) 289bool(false) 290int(53) 291bool(true) 292 293-- Testing fgetcsv() with file opened using x+t mode -- 294int(0) 295bool(false) 296bool(false) 297int(%d) 298bool(true) 299 300-- Testing fgetcsv() with file opened using r mode -- 301int(0) 302bool(false) 303bool(false) 304int(57) 305bool(true) 306 307-- Testing fgetcsv() with file opened using rb mode -- 308int(0) 309bool(false) 310bool(false) 311int(57) 312bool(true) 313 314-- Testing fgetcsv() with file opened using rt mode -- 315int(0) 316bool(false) 317bool(false) 318int(%d) 319bool(true) 320 321-- Testing fgetcsv() with file opened using r+ mode -- 322int(0) 323bool(false) 324bool(false) 325int(57) 326bool(true) 327 328-- Testing fgetcsv() with file opened using r+b mode -- 329int(0) 330bool(false) 331bool(false) 332int(57) 333bool(true) 334 335-- Testing fgetcsv() with file opened using r+t mode -- 336int(0) 337bool(false) 338bool(false) 339int(%d) 340bool(true) 341 342-- Testing fgetcsv() with file opened using a+ mode -- 343int(0) 344bool(false) 345bool(false) 346int(57) 347bool(true) 348 349-- Testing fgetcsv() with file opened using a+b mode -- 350int(0) 351bool(false) 352bool(false) 353int(57) 354bool(true) 355 356-- Testing fgetcsv() with file opened using a+t mode -- 357int(0) 358bool(false) 359bool(false) 360int(%d) 361bool(true) 362 363-- Testing fgetcsv() with file opened using w+ mode -- 364int(0) 365bool(false) 366bool(false) 367int(57) 368bool(true) 369 370-- Testing fgetcsv() with file opened using w+b mode -- 371int(0) 372bool(false) 373bool(false) 374int(57) 375bool(true) 376 377-- Testing fgetcsv() with file opened using w+t mode -- 378int(0) 379bool(false) 380bool(false) 381int(%d) 382bool(true) 383 384-- Testing fgetcsv() with file opened using x+ mode -- 385int(0) 386bool(false) 387bool(false) 388int(57) 389bool(true) 390 391-- Testing fgetcsv() with file opened using x+b mode -- 392int(0) 393bool(false) 394bool(false) 395int(57) 396bool(true) 397 398-- Testing fgetcsv() with file opened using x+t mode -- 399int(0) 400bool(false) 401bool(false) 402int(%d) 403bool(true) 404 405-- Testing fgetcsv() with file opened using r mode -- 406int(0) 407bool(false) 408bool(false) 409int(61) 410bool(true) 411 412-- Testing fgetcsv() with file opened using rb mode -- 413int(0) 414bool(false) 415bool(false) 416int(61) 417bool(true) 418 419-- Testing fgetcsv() with file opened using rt mode -- 420int(0) 421bool(false) 422bool(false) 423int(%d) 424bool(true) 425 426-- Testing fgetcsv() with file opened using r+ mode -- 427int(0) 428bool(false) 429bool(false) 430int(61) 431bool(true) 432 433-- Testing fgetcsv() with file opened using r+b mode -- 434int(0) 435bool(false) 436bool(false) 437int(61) 438bool(true) 439 440-- Testing fgetcsv() with file opened using r+t mode -- 441int(0) 442bool(false) 443bool(false) 444int(%d) 445bool(true) 446 447-- Testing fgetcsv() with file opened using a+ mode -- 448int(0) 449bool(false) 450bool(false) 451int(61) 452bool(true) 453 454-- Testing fgetcsv() with file opened using a+b mode -- 455int(0) 456bool(false) 457bool(false) 458int(61) 459bool(true) 460 461-- Testing fgetcsv() with file opened using a+t mode -- 462int(0) 463bool(false) 464bool(false) 465int(%d) 466bool(true) 467 468-- Testing fgetcsv() with file opened using w+ mode -- 469int(0) 470bool(false) 471bool(false) 472int(61) 473bool(true) 474 475-- Testing fgetcsv() with file opened using w+b mode -- 476int(0) 477bool(false) 478bool(false) 479int(61) 480bool(true) 481 482-- Testing fgetcsv() with file opened using w+t mode -- 483int(0) 484bool(false) 485bool(false) 486int(%d) 487bool(true) 488 489-- Testing fgetcsv() with file opened using x+ mode -- 490int(0) 491bool(false) 492bool(false) 493int(61) 494bool(true) 495 496-- Testing fgetcsv() with file opened using x+b mode -- 497int(0) 498bool(false) 499bool(false) 500int(61) 501bool(true) 502 503-- Testing fgetcsv() with file opened using x+t mode -- 504int(0) 505bool(false) 506bool(false) 507int(%d) 508bool(true) 509 510-- Testing fgetcsv() with file opened using r mode -- 511int(0) 512bool(false) 513bool(false) 514int(61) 515bool(true) 516 517-- Testing fgetcsv() with file opened using rb mode -- 518int(0) 519bool(false) 520bool(false) 521int(61) 522bool(true) 523 524-- Testing fgetcsv() with file opened using rt mode -- 525int(0) 526bool(false) 527bool(false) 528int(%d) 529bool(true) 530 531-- Testing fgetcsv() with file opened using r+ mode -- 532int(0) 533bool(false) 534bool(false) 535int(61) 536bool(true) 537 538-- Testing fgetcsv() with file opened using r+b mode -- 539int(0) 540bool(false) 541bool(false) 542int(61) 543bool(true) 544 545-- Testing fgetcsv() with file opened using r+t mode -- 546int(0) 547bool(false) 548bool(false) 549int(%d) 550bool(true) 551 552-- Testing fgetcsv() with file opened using a+ mode -- 553int(0) 554bool(false) 555bool(false) 556int(61) 557bool(true) 558 559-- Testing fgetcsv() with file opened using a+b mode -- 560int(0) 561bool(false) 562bool(false) 563int(61) 564bool(true) 565 566-- Testing fgetcsv() with file opened using a+t mode -- 567int(0) 568bool(false) 569bool(false) 570int(%d) 571bool(true) 572 573-- Testing fgetcsv() with file opened using w+ mode -- 574int(0) 575bool(false) 576bool(false) 577int(61) 578bool(true) 579 580-- Testing fgetcsv() with file opened using w+b mode -- 581int(0) 582bool(false) 583bool(false) 584int(61) 585bool(true) 586 587-- Testing fgetcsv() with file opened using w+t mode -- 588int(0) 589bool(false) 590bool(false) 591int(%d) 592bool(true) 593 594-- Testing fgetcsv() with file opened using x+ mode -- 595int(0) 596bool(false) 597bool(false) 598int(61) 599bool(true) 600 601-- Testing fgetcsv() with file opened using x+b mode -- 602int(0) 603bool(false) 604bool(false) 605int(61) 606bool(true) 607 608-- Testing fgetcsv() with file opened using x+t mode -- 609int(0) 610bool(false) 611bool(false) 612int(%d) 613bool(true) 614Done 615