1--TEST-- 2fread & fwrite - Test reading and writing using a single resource 3--CREDITS-- 4Dave Kelsey <d_kelsey@uk.ibm.com> 5--FILE-- 6<?php 7 8/* 9 * proto int fwrite(resource fp, string str [, int length]) 10 * Function is implemented in ext/standard/file.c 11 */ 12 13 /* 14 Prototype: string fread ( resource $handle [, int $length] ); 15 Description: reads up to length bytes from the file pointer referenced by handle. 16 Reading stops when up to length bytes have been read, EOF (end of file) is 17 reached, (for network streams) when a packet becomes available, or (after 18 opening userspace stream) when 8192 bytes have been read whichever comes first. 19*/ 20 21 22$outputfile = __FILE__.".tmp"; 23 24echo "--- testing rw moving about the file ---\n"; 25$h = fopen($outputfile, 'wb+'); 26$out1 = "The 1st prrt"; 27$out2 = " part of the ttxt"; 28$out3 = "text"; 29fwrite($h, $out1); 30fseek($h, 0, SEEK_SET); 31echo "start:".fread($h, strlen($out1) - 5). "\n"; 32fwrite($h, $out2); 33echo "at end:".fread($h,100)."\n"; 34var_dump(feof($h)); 35fseek($h, -4, SEEK_CUR); 36fwrite($h, $out3); 37fseek($h, 0, SEEK_SET); 38echo "final:".fread($h, 100)."\n"; 39fclose($h); 40 41echo "--- testing eof ---\n"; 42$h = fopen($outputfile, 'ab+'); 43fread($h,1024); 44var_dump(feof($h)); 45fread($h,1); 46var_dump(feof($h)); 47$out = "extra"; 48fwrite($h, $out); 49var_dump(feof($h)); 50fread($h,1); 51var_dump(feof($h)); 52fseek($h, -strlen($out) + 1, SEEK_CUR); 53echo "last bytes: ".fread($h, strlen($out))."\n"; 54fclose($h); 55 56unlink($outputfile); 57 58echo "Done"; 59?> 60--EXPECT-- 61--- testing rw moving about the file --- 62start:The 1st 63at end: 64bool(true) 65final:The 1st part of the text 66--- testing eof --- 67bool(true) 68bool(true) 69bool(true) 70bool(true) 71last bytes: xtra 72Done 73