1--TEST-- 2Test fgets() function : usage variations - read with/without length 3--FILE-- 4<?php 5/* 6 Prototype: string fgets ( resource $handle [, int $length] ); 7 Description: Gets a line from file pointer 8*/ 9 10// include the file.inc for common test funcitons 11include ("file.inc"); 12 13$file_modes = array("w+", "w+b", "w+t", 14 "a+", "a+b", "a+t", 15 "x+", "x+b", "x+t"); 16 17$file_content_types = array("numeric", "text", "text_with_new_line", "alphanumeric"); 18 19echo "*** Testing fgets() : usage variations ***\n"; 20 21$filename = dirname(__FILE__)."/fgets_variation3.tmp"; 22 23foreach($file_modes as $file_mode) { 24 echo "\n-- Testing fgets() with file opened using mode $file_mode --\n"; 25 26 foreach($file_content_types as $file_content_type) { 27 echo "-- File content type : $file_content_type --\n"; 28 29 /* create files with $file_content_type */ 30 $file_handle = fopen($filename, $file_mode); 31 $data = fill_file($file_handle, $file_content_type, 50); 32 33 if ( !$file_handle ) { 34 echo "Error: failed to open file $filename!"; 35 exit(); 36 } 37 38 echo "-- fgets() with default length, file pointer at 0 --\n"; 39 // get the file pointer to begining of the file 40 rewind($file_handle); 41 42 var_dump( ftell($file_handle) ); 43 var_dump( fgets($file_handle) ); // with default length 44 var_dump( ftell($file_handle) ); // ensure the file pointer position 45 var_dump( feof($file_handle) ); // enusre if eof set 46 47 echo "-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --\n"; 48 // get the file pointer to begining of the file 49 rewind($file_handle); 50 51 var_dump( ftell($file_handle) ); 52 var_dump( fgets($file_handle, 23) ); // expected: 22 chars 53 var_dump( ftell($file_handle) ); // ensure the file pointer position 54 var_dump( feof($file_handle) ); // enusre if eof set 55 56 //close file 57 fclose($file_handle); 58 59 // delete file 60 delete_file($filename); 61 } // file_content_type loop 62} // file_mode loop 63 64echo "Done\n"; 65?> 66--EXPECTF-- 67*** Testing fgets() : usage variations *** 68 69-- Testing fgets() with file opened using mode w+ -- 70-- File content type : numeric -- 71-- fgets() with default length, file pointer at 0 -- 72int(0) 73string(50) "22222222222222222222222222222222222222222222222222" 74int(50) 75bool(true) 76-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 77int(0) 78string(22) "2222222222222222222222" 79int(22) 80bool(false) 81-- File content type : text -- 82-- fgets() with default length, file pointer at 0 -- 83int(0) 84string(50) "text text text text text text text text text text " 85int(50) 86bool(true) 87-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 88int(0) 89string(22) "text text text text te" 90int(22) 91bool(false) 92-- File content type : text_with_new_line -- 93-- fgets() with default length, file pointer at 0 -- 94int(0) 95string(5) "line 96" 97int(5) 98bool(false) 99-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 100int(0) 101string(5) "line 102" 103int(5) 104bool(false) 105-- File content type : alphanumeric -- 106-- fgets() with default length, file pointer at 0 -- 107int(0) 108string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 109int(50) 110bool(true) 111-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 112int(0) 113string(22) "ab12 ab12 ab12 ab12 ab" 114int(22) 115bool(false) 116 117-- Testing fgets() with file opened using mode w+b -- 118-- File content type : numeric -- 119-- fgets() with default length, file pointer at 0 -- 120int(0) 121string(50) "22222222222222222222222222222222222222222222222222" 122int(50) 123bool(true) 124-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 125int(0) 126string(22) "2222222222222222222222" 127int(22) 128bool(false) 129-- File content type : text -- 130-- fgets() with default length, file pointer at 0 -- 131int(0) 132string(50) "text text text text text text text text text text " 133int(50) 134bool(true) 135-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 136int(0) 137string(22) "text text text text te" 138int(22) 139bool(false) 140-- File content type : text_with_new_line -- 141-- fgets() with default length, file pointer at 0 -- 142int(0) 143string(5) "line 144" 145int(5) 146bool(false) 147-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 148int(0) 149string(5) "line 150" 151int(5) 152bool(false) 153-- File content type : alphanumeric -- 154-- fgets() with default length, file pointer at 0 -- 155int(0) 156string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 157int(50) 158bool(true) 159-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 160int(0) 161string(22) "ab12 ab12 ab12 ab12 ab" 162int(22) 163bool(false) 164 165-- Testing fgets() with file opened using mode w+t -- 166-- File content type : numeric -- 167-- fgets() with default length, file pointer at 0 -- 168int(0) 169string(50) "22222222222222222222222222222222222222222222222222" 170int(50) 171bool(true) 172-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 173int(0) 174string(22) "2222222222222222222222" 175int(22) 176bool(false) 177-- File content type : text -- 178-- fgets() with default length, file pointer at 0 -- 179int(0) 180string(50) "text text text text text text text text text text " 181int(50) 182bool(true) 183-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 184int(0) 185string(22) "text text text text te" 186int(22) 187bool(false) 188-- File content type : text_with_new_line -- 189-- fgets() with default length, file pointer at 0 -- 190int(0) 191string(5) "line 192" 193int(5) 194bool(false) 195-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 196int(0) 197string(5) "line 198" 199int(5) 200bool(false) 201-- File content type : alphanumeric -- 202-- fgets() with default length, file pointer at 0 -- 203int(0) 204string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 205int(50) 206bool(true) 207-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 208int(0) 209string(22) "ab12 ab12 ab12 ab12 ab" 210int(22) 211bool(false) 212 213-- Testing fgets() with file opened using mode a+ -- 214-- File content type : numeric -- 215-- fgets() with default length, file pointer at 0 -- 216int(0) 217string(50) "22222222222222222222222222222222222222222222222222" 218int(50) 219bool(true) 220-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 221int(0) 222string(22) "2222222222222222222222" 223int(22) 224bool(false) 225-- File content type : text -- 226-- fgets() with default length, file pointer at 0 -- 227int(0) 228string(50) "text text text text text text text text text text " 229int(50) 230bool(true) 231-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 232int(0) 233string(22) "text text text text te" 234int(22) 235bool(false) 236-- File content type : text_with_new_line -- 237-- fgets() with default length, file pointer at 0 -- 238int(0) 239string(5) "line 240" 241int(5) 242bool(false) 243-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 244int(0) 245string(5) "line 246" 247int(5) 248bool(false) 249-- File content type : alphanumeric -- 250-- fgets() with default length, file pointer at 0 -- 251int(0) 252string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 253int(50) 254bool(true) 255-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 256int(0) 257string(22) "ab12 ab12 ab12 ab12 ab" 258int(22) 259bool(false) 260 261-- Testing fgets() with file opened using mode a+b -- 262-- File content type : numeric -- 263-- fgets() with default length, file pointer at 0 -- 264int(0) 265string(50) "22222222222222222222222222222222222222222222222222" 266int(50) 267bool(true) 268-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 269int(0) 270string(22) "2222222222222222222222" 271int(22) 272bool(false) 273-- File content type : text -- 274-- fgets() with default length, file pointer at 0 -- 275int(0) 276string(50) "text text text text text text text text text text " 277int(50) 278bool(true) 279-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 280int(0) 281string(22) "text text text text te" 282int(22) 283bool(false) 284-- File content type : text_with_new_line -- 285-- fgets() with default length, file pointer at 0 -- 286int(0) 287string(5) "line 288" 289int(5) 290bool(false) 291-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 292int(0) 293string(5) "line 294" 295int(5) 296bool(false) 297-- File content type : alphanumeric -- 298-- fgets() with default length, file pointer at 0 -- 299int(0) 300string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 301int(50) 302bool(true) 303-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 304int(0) 305string(22) "ab12 ab12 ab12 ab12 ab" 306int(22) 307bool(false) 308 309-- Testing fgets() with file opened using mode a+t -- 310-- File content type : numeric -- 311-- fgets() with default length, file pointer at 0 -- 312int(0) 313string(50) "22222222222222222222222222222222222222222222222222" 314int(50) 315bool(true) 316-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 317int(0) 318string(22) "2222222222222222222222" 319int(22) 320bool(false) 321-- File content type : text -- 322-- fgets() with default length, file pointer at 0 -- 323int(0) 324string(50) "text text text text text text text text text text " 325int(50) 326bool(true) 327-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 328int(0) 329string(22) "text text text text te" 330int(22) 331bool(false) 332-- File content type : text_with_new_line -- 333-- fgets() with default length, file pointer at 0 -- 334int(0) 335string(5) "line 336" 337int(5) 338bool(false) 339-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 340int(0) 341string(5) "line 342" 343int(5) 344bool(false) 345-- File content type : alphanumeric -- 346-- fgets() with default length, file pointer at 0 -- 347int(0) 348string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 349int(50) 350bool(true) 351-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 352int(0) 353string(22) "ab12 ab12 ab12 ab12 ab" 354int(22) 355bool(false) 356 357-- Testing fgets() with file opened using mode x+ -- 358-- File content type : numeric -- 359-- fgets() with default length, file pointer at 0 -- 360int(0) 361string(50) "22222222222222222222222222222222222222222222222222" 362int(50) 363bool(true) 364-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 365int(0) 366string(22) "2222222222222222222222" 367int(22) 368bool(false) 369-- File content type : text -- 370-- fgets() with default length, file pointer at 0 -- 371int(0) 372string(50) "text text text text text text text text text text " 373int(50) 374bool(true) 375-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 376int(0) 377string(22) "text text text text te" 378int(22) 379bool(false) 380-- File content type : text_with_new_line -- 381-- fgets() with default length, file pointer at 0 -- 382int(0) 383string(5) "line 384" 385int(5) 386bool(false) 387-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 388int(0) 389string(5) "line 390" 391int(5) 392bool(false) 393-- File content type : alphanumeric -- 394-- fgets() with default length, file pointer at 0 -- 395int(0) 396string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 397int(50) 398bool(true) 399-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 400int(0) 401string(22) "ab12 ab12 ab12 ab12 ab" 402int(22) 403bool(false) 404 405-- Testing fgets() with file opened using mode x+b -- 406-- File content type : numeric -- 407-- fgets() with default length, file pointer at 0 -- 408int(0) 409string(50) "22222222222222222222222222222222222222222222222222" 410int(50) 411bool(true) 412-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 413int(0) 414string(22) "2222222222222222222222" 415int(22) 416bool(false) 417-- File content type : text -- 418-- fgets() with default length, file pointer at 0 -- 419int(0) 420string(50) "text text text text text text text text text text " 421int(50) 422bool(true) 423-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 424int(0) 425string(22) "text text text text te" 426int(22) 427bool(false) 428-- File content type : text_with_new_line -- 429-- fgets() with default length, file pointer at 0 -- 430int(0) 431string(5) "line 432" 433int(5) 434bool(false) 435-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 436int(0) 437string(5) "line 438" 439int(5) 440bool(false) 441-- File content type : alphanumeric -- 442-- fgets() with default length, file pointer at 0 -- 443int(0) 444string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 445int(50) 446bool(true) 447-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 448int(0) 449string(22) "ab12 ab12 ab12 ab12 ab" 450int(22) 451bool(false) 452 453-- Testing fgets() with file opened using mode x+t -- 454-- File content type : numeric -- 455-- fgets() with default length, file pointer at 0 -- 456int(0) 457string(50) "22222222222222222222222222222222222222222222222222" 458int(50) 459bool(true) 460-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 461int(0) 462string(22) "2222222222222222222222" 463int(22) 464bool(false) 465-- File content type : text -- 466-- fgets() with default length, file pointer at 0 -- 467int(0) 468string(50) "text text text text text text text text text text " 469int(50) 470bool(true) 471-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 472int(0) 473string(22) "text text text text te" 474int(22) 475bool(false) 476-- File content type : text_with_new_line -- 477-- fgets() with default length, file pointer at 0 -- 478int(0) 479string(5) "line 480" 481int(5) 482bool(false) 483-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 484int(0) 485string(5) "line 486" 487int(5) 488bool(false) 489-- File content type : alphanumeric -- 490-- fgets() with default length, file pointer at 0 -- 491int(0) 492string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 493int(50) 494bool(true) 495-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 -- 496int(0) 497string(22) "ab12 ab12 ab12 ab12 ab" 498int(22) 499bool(false) 500Done