1--TEST-- 2Test fopen(), fclose() & feof() functions: basic functionality 3--FILE-- 4<?php 5/* 6 Prototype: resource fopen(string $filename, string $mode 7 [, bool $use_include_path [, resource $context]] ); 8 Description: Opens file or URL. 9 10 Prototype: bool fclose ( resource $handle ); 11 Description: Closes an open file pointer 12 13 Prototype: bool feof ( resource $handle ); 14 Description: Tests for end-of-file on a file pointer 15*/ 16 17echo "*** Testing basic operations of fopen() and fclose() functions ***\n"; 18$modes = array( 19 "w", 20 "wb", 21 "wt", 22 "w+", 23 "w+b", 24 "w+t", 25 26 "r", 27 "rb", 28 "rt", 29 "r+", 30 "r+b", 31 "r+t", 32 33 "a", 34 "ab", 35 "at", 36 "a+", 37 "a+t", 38 "a+b" 39); 40 41for( $i=0; $i<count($modes); $i++ ) { 42 echo "\n-- Iteration with mode '$modes[$i]' --\n"; 43 44 $filename = dirname(__FILE__)."/007_basic.tmp"; 45 // check fopen() 46 $handle = fopen($filename, $modes[$i]); 47 var_dump($handle ); 48 var_dump( ftell($handle) ); 49 var_dump( feof($handle) ); 50 51 // check fclose() 52 var_dump( fclose($handle) ); 53 var_dump( $handle ); 54 // confirm the closure, using ftell() and feof(), expect, false 55 var_dump( ftell($handle) ); 56 var_dump( feof($handle) ); 57} 58 59// remove the temp file 60unlink($filename); 61 62$x_modes = array( 63 "x", 64 "xb", 65 "xt", 66 "x+", 67 "x+b", 68 "x+t" 69); 70 71for( $i=0; $i<count($x_modes); $i++ ) { 72 echo "\n-- Iteration with mode '$x_modes[$i]' --\n"; 73 $handle = fopen($filename, $x_modes[$i]); 74 var_dump($handle ); 75 var_dump( ftell($handle) ); 76 var_dump( feof($handle) ); 77 78 // check fclose() 79 var_dump( fclose($handle) ); 80 var_dump( $handle ); 81 // confirm the closure, using ftell() and feof(), expect, false 82 var_dump( ftell($handle) ); 83 var_dump( feof($handle) ); 84 var_dump( $handle ); 85 86 // remove the file 87 unlink( $filename ); 88} 89 90echo "\n*** Done ***\n"; 91--EXPECTF-- 92*** Testing basic operations of fopen() and fclose() functions *** 93 94-- Iteration with mode 'w' -- 95resource(%d) of type (stream) 96int(0) 97bool(false) 98bool(true) 99resource(%d) of type (Unknown) 100 101Warning: ftell(): %d is not a valid stream resource in %s on line %d 102bool(false) 103 104Warning: feof(): %d is not a valid stream resource in %s on line %d 105bool(false) 106 107-- Iteration with mode 'wb' -- 108resource(%d) of type (stream) 109int(0) 110bool(false) 111bool(true) 112resource(%d) of type (Unknown) 113 114Warning: ftell(): %d is not a valid stream resource in %s on line %d 115bool(false) 116 117Warning: feof(): %d is not a valid stream resource in %s on line %d 118bool(false) 119 120-- Iteration with mode 'wt' -- 121resource(%d) of type (stream) 122int(0) 123bool(false) 124bool(true) 125resource(%d) of type (Unknown) 126 127Warning: ftell(): %d is not a valid stream resource in %s on line %d 128bool(false) 129 130Warning: feof(): %d is not a valid stream resource in %s on line %d 131bool(false) 132 133-- Iteration with mode 'w+' -- 134resource(%d) of type (stream) 135int(0) 136bool(false) 137bool(true) 138resource(%d) of type (Unknown) 139 140Warning: ftell(): %d is not a valid stream resource in %s on line %d 141bool(false) 142 143Warning: feof(): %d is not a valid stream resource in %s on line %d 144bool(false) 145 146-- Iteration with mode 'w+b' -- 147resource(%d) of type (stream) 148int(0) 149bool(false) 150bool(true) 151resource(%d) of type (Unknown) 152 153Warning: ftell(): %d is not a valid stream resource in %s on line %d 154bool(false) 155 156Warning: feof(): %d is not a valid stream resource in %s on line %d 157bool(false) 158 159-- Iteration with mode 'w+t' -- 160resource(%d) of type (stream) 161int(0) 162bool(false) 163bool(true) 164resource(%d) of type (Unknown) 165 166Warning: ftell(): %d is not a valid stream resource in %s on line %d 167bool(false) 168 169Warning: feof(): %d is not a valid stream resource in %s on line %d 170bool(false) 171 172-- Iteration with mode 'r' -- 173resource(%d) of type (stream) 174int(0) 175bool(false) 176bool(true) 177resource(%d) of type (Unknown) 178 179Warning: ftell(): %d is not a valid stream resource in %s on line %d 180bool(false) 181 182Warning: feof(): %d is not a valid stream resource in %s on line %d 183bool(false) 184 185-- Iteration with mode 'rb' -- 186resource(%d) of type (stream) 187int(0) 188bool(false) 189bool(true) 190resource(%d) of type (Unknown) 191 192Warning: ftell(): %d is not a valid stream resource in %s on line %d 193bool(false) 194 195Warning: feof(): %d is not a valid stream resource in %s on line %d 196bool(false) 197 198-- Iteration with mode 'rt' -- 199resource(%d) of type (stream) 200int(0) 201bool(false) 202bool(true) 203resource(%d) of type (Unknown) 204 205Warning: ftell(): %d is not a valid stream resource in %s on line %d 206bool(false) 207 208Warning: feof(): %d is not a valid stream resource in %s on line %d 209bool(false) 210 211-- Iteration with mode 'r+' -- 212resource(%d) of type (stream) 213int(0) 214bool(false) 215bool(true) 216resource(%d) of type (Unknown) 217 218Warning: ftell(): %d is not a valid stream resource in %s on line %d 219bool(false) 220 221Warning: feof(): %d is not a valid stream resource in %s on line %d 222bool(false) 223 224-- Iteration with mode 'r+b' -- 225resource(%d) of type (stream) 226int(0) 227bool(false) 228bool(true) 229resource(%d) of type (Unknown) 230 231Warning: ftell(): %d is not a valid stream resource in %s on line %d 232bool(false) 233 234Warning: feof(): %d is not a valid stream resource in %s on line %d 235bool(false) 236 237-- Iteration with mode 'r+t' -- 238resource(%d) of type (stream) 239int(0) 240bool(false) 241bool(true) 242resource(%d) of type (Unknown) 243 244Warning: ftell(): %d is not a valid stream resource in %s on line %d 245bool(false) 246 247Warning: feof(): %d is not a valid stream resource in %s on line %d 248bool(false) 249 250-- Iteration with mode 'a' -- 251resource(%d) of type (stream) 252int(0) 253bool(false) 254bool(true) 255resource(%d) of type (Unknown) 256 257Warning: ftell(): %d is not a valid stream resource in %s on line %d 258bool(false) 259 260Warning: feof(): %d is not a valid stream resource in %s on line %d 261bool(false) 262 263-- Iteration with mode 'ab' -- 264resource(%d) of type (stream) 265int(0) 266bool(false) 267bool(true) 268resource(%d) of type (Unknown) 269 270Warning: ftell(): %d is not a valid stream resource in %s on line %d 271bool(false) 272 273Warning: feof(): %d is not a valid stream resource in %s on line %d 274bool(false) 275 276-- Iteration with mode 'at' -- 277resource(%d) of type (stream) 278int(0) 279bool(false) 280bool(true) 281resource(%d) of type (Unknown) 282 283Warning: ftell(): %d is not a valid stream resource in %s on line %d 284bool(false) 285 286Warning: feof(): %d is not a valid stream resource in %s on line %d 287bool(false) 288 289-- Iteration with mode 'a+' -- 290resource(%d) of type (stream) 291int(0) 292bool(false) 293bool(true) 294resource(%d) of type (Unknown) 295 296Warning: ftell(): %d is not a valid stream resource in %s on line %d 297bool(false) 298 299Warning: feof(): %d is not a valid stream resource in %s on line %d 300bool(false) 301 302-- Iteration with mode 'a+t' -- 303resource(%d) of type (stream) 304int(0) 305bool(false) 306bool(true) 307resource(%d) of type (Unknown) 308 309Warning: ftell(): %d is not a valid stream resource in %s on line %d 310bool(false) 311 312Warning: feof(): %d is not a valid stream resource in %s on line %d 313bool(false) 314 315-- Iteration with mode 'a+b' -- 316resource(%d) of type (stream) 317int(0) 318bool(false) 319bool(true) 320resource(%d) of type (Unknown) 321 322Warning: ftell(): %d is not a valid stream resource in %s on line %d 323bool(false) 324 325Warning: feof(): %d is not a valid stream resource in %s on line %d 326bool(false) 327 328-- Iteration with mode 'x' -- 329resource(%d) of type (stream) 330int(0) 331bool(false) 332bool(true) 333resource(%d) of type (Unknown) 334 335Warning: ftell(): %d is not a valid stream resource in %s on line %d 336bool(false) 337 338Warning: feof(): %d is not a valid stream resource in %s on line %d 339bool(false) 340resource(%d) of type (Unknown) 341 342-- Iteration with mode 'xb' -- 343resource(%d) of type (stream) 344int(0) 345bool(false) 346bool(true) 347resource(%d) of type (Unknown) 348 349Warning: ftell(): %d is not a valid stream resource in %s on line %d 350bool(false) 351 352Warning: feof(): %d is not a valid stream resource in %s on line %d 353bool(false) 354resource(%d) of type (Unknown) 355 356-- Iteration with mode 'xt' -- 357resource(%d) of type (stream) 358int(0) 359bool(false) 360bool(true) 361resource(%d) of type (Unknown) 362 363Warning: ftell(): %d is not a valid stream resource in %s on line %d 364bool(false) 365 366Warning: feof(): %d is not a valid stream resource in %s on line %d 367bool(false) 368resource(%d) of type (Unknown) 369 370-- Iteration with mode 'x+' -- 371resource(%d) of type (stream) 372int(0) 373bool(false) 374bool(true) 375resource(%d) of type (Unknown) 376 377Warning: ftell(): %d is not a valid stream resource in %s on line %d 378bool(false) 379 380Warning: feof(): %d is not a valid stream resource in %s on line %d 381bool(false) 382resource(%d) of type (Unknown) 383 384-- Iteration with mode 'x+b' -- 385resource(%d) of type (stream) 386int(0) 387bool(false) 388bool(true) 389resource(%d) of type (Unknown) 390 391Warning: ftell(): %d is not a valid stream resource in %s on line %d 392bool(false) 393 394Warning: feof(): %d is not a valid stream resource in %s on line %d 395bool(false) 396resource(%d) of type (Unknown) 397 398-- Iteration with mode 'x+t' -- 399resource(%d) of type (stream) 400int(0) 401bool(false) 402bool(true) 403resource(%d) of type (Unknown) 404 405Warning: ftell(): %d is not a valid stream resource in %s on line %d 406bool(false) 407 408Warning: feof(): %d is not a valid stream resource in %s on line %d 409bool(false) 410resource(%d) of type (Unknown) 411 412*** Done *** 413