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