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