2 Test fseek(), ftell() & rewind() functions : usage variations - all r & a modes, default whence
5 /* Prototype: int fseek ( resource $handle, int $offset [, int $whence] );
6 Description: Seeks on a file pointer
8 Prototype: bool rewind ( resource $handle );
9 Description: Rewind the position of a file pointer
11 Prototype: int ftell ( resource $handle );
12 Description: Tells file pointer read/write position
15 // include the file.inc for common functions for test
18 /* Testing fseek(),ftell(),rewind() functions
19 1. All read and append modes
20 2. Testing fseek() without using argument whence
22 echo "*** Testing fseek(), ftell(), rewind() : default whence & all r and a modes ***\n";
23 $file_modes = array( "r","rb","rt","r+","r+b","r+t",
24 "a","ab","at","a+","a+b","a+t");
25 $file_content_types = array( "text_with_new_line","alphanumeric");
27 $offset = array(-1, 0, 1, 513); // different offsets, including negative and beyond size
29 $filename = dirname(__FILE__)."/fseek_ftell_rewind_variation1.tmp"; // this is name of the file created by create_files()
31 /* open the file using $files_modes and perform fseek(),ftell() and rewind() on it */
32 foreach($file_content_types as $file_content_type){
33 echo "\n-- File having data of type ". $file_content_type ." --\n";
35 foreach($file_modes as $file_mode) {
36 echo "-- File opened in mode ".$file_mode." --\n";
38 create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 512, "w", "fseek_ftell_rewind_variation"
39 ,1,"bytes",".tmp"); //create a file with 512 bytes size
40 $file_handle = fopen($filename, $file_mode);
42 echo "Error: failed to fopen() file: $filename!";
45 echo "-- Testing fseek() without using argument whence --\n";
46 foreach($offset as $count){
47 var_dump( fseek($file_handle, $count) );
48 var_dump( ftell($file_handle) ); // confirm the file pointer position
49 var_dump( feof($file_handle) ); //ensure that file pointer is not at end
50 } //end of offset loop
52 //close the file and check the size
54 var_dump( filesize($filename) );
56 delete_file($filename); // delete file with name
57 } //end of file_mode loop
58 } //end of file_content_types loop
63 *** Testing fseek(), ftell(), rewind() : default whence & all r and a modes ***
65 -- File having data of type text_with_new_line --
66 -- File opened in mode r --
67 -- Testing fseek() without using argument whence --
81 -- File opened in mode rb --
82 -- Testing fseek() without using argument whence --
96 -- File opened in mode rt --
97 -- Testing fseek() without using argument whence --
111 -- File opened in mode r+ --
112 -- Testing fseek() without using argument whence --
126 -- File opened in mode r+b --
127 -- Testing fseek() without using argument whence --
141 -- File opened in mode r+t --
142 -- Testing fseek() without using argument whence --
156 -- File opened in mode a --
157 -- Testing fseek() without using argument whence --
171 -- File opened in mode ab --
172 -- Testing fseek() without using argument whence --
186 -- File opened in mode at --
187 -- Testing fseek() without using argument whence --
201 -- File opened in mode a+ --
202 -- Testing fseek() without using argument whence --
216 -- File opened in mode a+b --
217 -- Testing fseek() without using argument whence --
231 -- File opened in mode a+t --
232 -- Testing fseek() without using argument whence --
247 -- File having data of type alphanumeric --
248 -- File opened in mode r --
249 -- Testing fseek() without using argument whence --
263 -- File opened in mode rb --
264 -- Testing fseek() without using argument whence --
278 -- File opened in mode rt --
279 -- Testing fseek() without using argument whence --
293 -- File opened in mode r+ --
294 -- Testing fseek() without using argument whence --
308 -- File opened in mode r+b --
309 -- Testing fseek() without using argument whence --
323 -- File opened in mode r+t --
324 -- Testing fseek() without using argument whence --
338 -- File opened in mode a --
339 -- Testing fseek() without using argument whence --
353 -- File opened in mode ab --
354 -- Testing fseek() without using argument whence --
368 -- File opened in mode at --
369 -- Testing fseek() without using argument whence --
383 -- File opened in mode a+ --
384 -- Testing fseek() without using argument whence --
398 -- File opened in mode a+b --
399 -- Testing fseek() without using argument whence --
413 -- File opened in mode a+t --
414 -- Testing fseek() without using argument whence --