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