1--TEST-- 2Test feof() function : basic functionality 3--CREDITS-- 4Dave Kelsey <d_kelsey@uk.ibm.com> 5--FILE-- 6<?php 7/* Prototype : proto bool feof(resource fp) 8 * Description: Test for end-of-file on a file pointer 9 * Source code: ext/standard/file.c 10 * Alias to functions: gzeof 11 */ 12 13echo "*** Testing feof() : basic functionality ***\n"; 14$tmpFile1 = __FILE__.".tmp1"; 15$h = fopen($tmpFile1, 'wb'); 16$count = 10; 17for ($i = 1; $i <= $count; $i++) { 18 fwrite($h, "some data $i\n"); 19} 20fclose($h); 21 22echo "\n*** testing reading complete file using feof to stop ***\n"; 23$h = fopen($tmpFile1, 'rb'); 24 25//feof is not set to true until you try to read past the end of file. 26//so fgets will be called even if we are at the end of the file on 27//last time to set the eof flag but it will fail to read. 28$lastline = ""; 29while (!feof($h)) { 30 $previousLine = $lastline; 31 $lastline = fgets($h); 32} 33echo $previousLine; 34var_dump($lastline); // this should be false 35fclose($h); 36 37$tmpFile2 = __FILE__.".tmp2"; 38$h = fopen($tmpFile2, 'wb+'); 39$count = 10; 40echo "*** writing $count lines, testing feof ***\n"; 41for ($i = 1; $i <=$count; $i++) { 42 fwrite($h, "some data $i\n"); 43 var_dump(feof($h)); 44} 45 46echo "*** testing feof on unclosed file after a read ***\n"; 47 48fread($h, 100); 49var_dump(feof($h)); 50 51$eofPointer = ftell($h); 52 53echo "*** testing feof after a seek to near the beginning ***\n"; 54fseek($h, 20, SEEK_SET); 55var_dump(feof($h)); 56 57echo "*** testing feof after a seek to end ***\n"; 58fseek($h, $eofPointer, SEEK_SET); 59var_dump(feof($h)); 60 61echo "*** testing feof after a seek passed the end ***\n"; 62fseek($h, $eofPointer + 1000, SEEK_SET); 63var_dump(feof($h)); 64 65echo "*** closing file, testing eof ***\n"; 66fclose($h); 67feof($h); 68unlink($tmpFile1); 69unlink($tmpFile2); 70 71echo "Done"; 72?> 73--EXPECTF-- 74*** Testing feof() : basic functionality *** 75 76*** testing reading complete file using feof to stop *** 77some data 10 78bool(false) 79*** writing 10 lines, testing feof *** 80bool(false) 81bool(false) 82bool(false) 83bool(false) 84bool(false) 85bool(false) 86bool(false) 87bool(false) 88bool(false) 89bool(false) 90*** testing feof on unclosed file after a read *** 91bool(true) 92*** testing feof after a seek to near the beginning *** 93bool(false) 94*** testing feof after a seek to end *** 95bool(false) 96*** testing feof after a seek passed the end *** 97bool(false) 98*** closing file, testing eof *** 99 100Warning: feof(): supplied resource is not a valid stream resource in %s on line %d 101Done 102