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