1--TEST-- 2Test fgets() function : usage variations - read beyond filesize 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_variation5.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 /* read with length beyong file size */ 39 echo "-- fgets() with length > filesize --\n"; 40 rewind($file_handle); 41 42 var_dump( ftell($file_handle) ); 43 var_dump( fgets($file_handle, 50 + 23) ); // expected: 50 44 var_dump( ftell($file_handle) ); // ensure the file pointer position 45 var_dump( feof($file_handle) ); // enusre if eof set 46 47 //close file 48 fclose($file_handle); 49 50 // delete file 51 delete_file($filename); 52 } // file_content_type loop 53} // file_mode loop 54 55echo "Done\n"; 56?> 57--EXPECTF-- 58*** Testing fgets() : usage variations *** 59 60-- Testing fgets() with file opened using mode w+ -- 61-- File content type : numeric -- 62-- fgets() with length > filesize -- 63int(0) 64string(50) "22222222222222222222222222222222222222222222222222" 65int(50) 66bool(true) 67-- File content type : text -- 68-- fgets() with length > filesize -- 69int(0) 70string(50) "text text text text text text text text text text " 71int(50) 72bool(true) 73-- File content type : text_with_new_line -- 74-- fgets() with length > filesize -- 75int(0) 76string(5) "line 77" 78int(5) 79bool(false) 80-- File content type : alphanumeric -- 81-- fgets() with length > filesize -- 82int(0) 83string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 84int(50) 85bool(true) 86 87-- Testing fgets() with file opened using mode w+b -- 88-- File content type : numeric -- 89-- fgets() with length > filesize -- 90int(0) 91string(50) "22222222222222222222222222222222222222222222222222" 92int(50) 93bool(true) 94-- File content type : text -- 95-- fgets() with length > filesize -- 96int(0) 97string(50) "text text text text text text text text text text " 98int(50) 99bool(true) 100-- File content type : text_with_new_line -- 101-- fgets() with length > filesize -- 102int(0) 103string(5) "line 104" 105int(5) 106bool(false) 107-- File content type : alphanumeric -- 108-- fgets() with length > filesize -- 109int(0) 110string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 111int(50) 112bool(true) 113 114-- Testing fgets() with file opened using mode w+t -- 115-- File content type : numeric -- 116-- fgets() with length > filesize -- 117int(0) 118string(50) "22222222222222222222222222222222222222222222222222" 119int(50) 120bool(true) 121-- File content type : text -- 122-- fgets() with length > filesize -- 123int(0) 124string(50) "text text text text text text text text text text " 125int(50) 126bool(true) 127-- File content type : text_with_new_line -- 128-- fgets() with length > filesize -- 129int(0) 130string(5) "line 131" 132int(5) 133bool(false) 134-- File content type : alphanumeric -- 135-- fgets() with length > filesize -- 136int(0) 137string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 138int(50) 139bool(true) 140 141-- Testing fgets() with file opened using mode a+ -- 142-- File content type : numeric -- 143-- fgets() with length > filesize -- 144int(0) 145string(50) "22222222222222222222222222222222222222222222222222" 146int(50) 147bool(true) 148-- File content type : text -- 149-- fgets() with length > filesize -- 150int(0) 151string(50) "text text text text text text text text text text " 152int(50) 153bool(true) 154-- File content type : text_with_new_line -- 155-- fgets() with length > filesize -- 156int(0) 157string(5) "line 158" 159int(5) 160bool(false) 161-- File content type : alphanumeric -- 162-- fgets() with length > filesize -- 163int(0) 164string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 165int(50) 166bool(true) 167 168-- Testing fgets() with file opened using mode a+b -- 169-- File content type : numeric -- 170-- fgets() with length > filesize -- 171int(0) 172string(50) "22222222222222222222222222222222222222222222222222" 173int(50) 174bool(true) 175-- File content type : text -- 176-- fgets() with length > filesize -- 177int(0) 178string(50) "text text text text text text text text text text " 179int(50) 180bool(true) 181-- File content type : text_with_new_line -- 182-- fgets() with length > filesize -- 183int(0) 184string(5) "line 185" 186int(5) 187bool(false) 188-- File content type : alphanumeric -- 189-- fgets() with length > filesize -- 190int(0) 191string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 192int(50) 193bool(true) 194 195-- Testing fgets() with file opened using mode a+t -- 196-- File content type : numeric -- 197-- fgets() with length > filesize -- 198int(0) 199string(50) "22222222222222222222222222222222222222222222222222" 200int(50) 201bool(true) 202-- File content type : text -- 203-- fgets() with length > filesize -- 204int(0) 205string(50) "text text text text text text text text text text " 206int(50) 207bool(true) 208-- File content type : text_with_new_line -- 209-- fgets() with length > filesize -- 210int(0) 211string(5) "line 212" 213int(5) 214bool(false) 215-- File content type : alphanumeric -- 216-- fgets() with length > filesize -- 217int(0) 218string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 219int(50) 220bool(true) 221 222-- Testing fgets() with file opened using mode x+ -- 223-- File content type : numeric -- 224-- fgets() with length > filesize -- 225int(0) 226string(50) "22222222222222222222222222222222222222222222222222" 227int(50) 228bool(true) 229-- File content type : text -- 230-- fgets() with length > filesize -- 231int(0) 232string(50) "text text text text text text text text text text " 233int(50) 234bool(true) 235-- File content type : text_with_new_line -- 236-- fgets() with length > filesize -- 237int(0) 238string(5) "line 239" 240int(5) 241bool(false) 242-- File content type : alphanumeric -- 243-- fgets() with length > filesize -- 244int(0) 245string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 246int(50) 247bool(true) 248 249-- Testing fgets() with file opened using mode x+b -- 250-- File content type : numeric -- 251-- fgets() with length > filesize -- 252int(0) 253string(50) "22222222222222222222222222222222222222222222222222" 254int(50) 255bool(true) 256-- File content type : text -- 257-- fgets() with length > filesize -- 258int(0) 259string(50) "text text text text text text text text text text " 260int(50) 261bool(true) 262-- File content type : text_with_new_line -- 263-- fgets() with length > filesize -- 264int(0) 265string(5) "line 266" 267int(5) 268bool(false) 269-- File content type : alphanumeric -- 270-- fgets() with length > filesize -- 271int(0) 272string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 273int(50) 274bool(true) 275 276-- Testing fgets() with file opened using mode x+t -- 277-- File content type : numeric -- 278-- fgets() with length > filesize -- 279int(0) 280string(50) "22222222222222222222222222222222222222222222222222" 281int(50) 282bool(true) 283-- File content type : text -- 284-- fgets() with length > filesize -- 285int(0) 286string(50) "text text text text text text text text text text " 287int(50) 288bool(true) 289-- File content type : text_with_new_line -- 290-- fgets() with length > filesize -- 291int(0) 292string(5) "line 293" 294int(5) 295bool(false) 296-- File content type : alphanumeric -- 297-- fgets() with length > filesize -- 298int(0) 299string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 " 300int(50) 301bool(true) 302Done 303