1--TEST-- 2Test fseek(), ftell() & rewind() functions : Basic functionality - all r and a modes 3--FILE-- 4<?php 5/* Prototype: int fseek ( resource $handle, int $offset [, int $whence] ); 6 Description: Seeks on a file pointer 7 8 Prototype: bool rewind ( resource $handle ); 9 Description: Rewind the position of a file pointer 10 11 Prototype: int ftell ( resource $handle ); 12 Description: Tells file pointer read/write position 13*/ 14 15// include the file.inc for common functions for test 16include ("file.inc"); 17 18/* Testing fseek(),ftell(),rewind() functions on all read and append modes */ 19echo "*** Testing fseek(), ftell(), rewind() : basic operations ***\n"; 20$file_modes = array( "r","rb","rt","r+","r+b","r+t", 21 "a","ab","at","a+","a+b","a+t"); 22$file_content_types = array( "text_with_new_line","alphanumeric"); 23 24$whence_set = array(SEEK_SET,SEEK_CUR,SEEK_END); 25$whence_string = array("SEEK_SET", "SEEK_CUR", "SEEK_END"); 26 27$filename = __DIR__."/fseek_ftell_rewind_basic1.tmp"; // this is name of the file created by create_files() 28 /* open the file using $files_modes and perform fseek(),ftell() and rewind() on it */ 29foreach($file_content_types as $file_content_type){ 30 echo "\n-- File having data of type ". $file_content_type ." --\n"; 31 32 foreach($file_modes as $file_mode) { 33 echo "-- File opened in mode ".$file_mode." --\n"; 34 35 create_files ( __DIR__, 1, $file_content_type, 0755, 512, "w", "fseek_ftell_rewind_basic" 36 ,1,"bytes",".tmp"); //create a file with 512 bytes size 37 $file_handle = fopen($filename, $file_mode); 38 if (!$file_handle) { 39 echo "Error: failed to fopen() file: $filename!"; 40 exit(); 41 } 42 // set the file pointer to 0 43 var_dump( rewind($file_handle) ); // Confirm file pointer moves correctly 44 var_dump( ftell($file_handle) ); // confirm the file pointer position 45 46 foreach($whence_set as $whence){ 47 echo "-- Testing fseek() with whence = $whence_string[$whence] --\n"; 48 var_dump( fseek($file_handle,10,$whence) ); //expecting int(0) 49 var_dump( ftell($file_handle) ); // confirm the file pointer position 50 var_dump( feof($file_handle) ); //ensure that file pointer is not at end 51 } //end of whence loop 52 53 //close the file and check the size 54 fclose($file_handle); 55 var_dump( filesize($filename) ); 56 57 delete_file($filename); // delete file with name 58 } //end of file_modes loop 59} //end of file_content_types loop 60 61echo "Done\n"; 62?> 63--EXPECT-- 64*** Testing fseek(), ftell(), rewind() : basic operations *** 65 66-- File having data of type text_with_new_line -- 67-- File opened in mode r -- 68bool(true) 69int(0) 70-- Testing fseek() with whence = SEEK_SET -- 71int(0) 72int(10) 73bool(false) 74-- Testing fseek() with whence = SEEK_CUR -- 75int(0) 76int(20) 77bool(false) 78-- Testing fseek() with whence = SEEK_END -- 79int(0) 80int(522) 81bool(false) 82int(512) 83-- File opened in mode rb -- 84bool(true) 85int(0) 86-- Testing fseek() with whence = SEEK_SET -- 87int(0) 88int(10) 89bool(false) 90-- Testing fseek() with whence = SEEK_CUR -- 91int(0) 92int(20) 93bool(false) 94-- Testing fseek() with whence = SEEK_END -- 95int(0) 96int(522) 97bool(false) 98int(512) 99-- File opened in mode rt -- 100bool(true) 101int(0) 102-- Testing fseek() with whence = SEEK_SET -- 103int(0) 104int(10) 105bool(false) 106-- Testing fseek() with whence = SEEK_CUR -- 107int(0) 108int(20) 109bool(false) 110-- Testing fseek() with whence = SEEK_END -- 111int(0) 112int(522) 113bool(false) 114int(512) 115-- File opened in mode r+ -- 116bool(true) 117int(0) 118-- Testing fseek() with whence = SEEK_SET -- 119int(0) 120int(10) 121bool(false) 122-- Testing fseek() with whence = SEEK_CUR -- 123int(0) 124int(20) 125bool(false) 126-- Testing fseek() with whence = SEEK_END -- 127int(0) 128int(522) 129bool(false) 130int(512) 131-- File opened in mode r+b -- 132bool(true) 133int(0) 134-- Testing fseek() with whence = SEEK_SET -- 135int(0) 136int(10) 137bool(false) 138-- Testing fseek() with whence = SEEK_CUR -- 139int(0) 140int(20) 141bool(false) 142-- Testing fseek() with whence = SEEK_END -- 143int(0) 144int(522) 145bool(false) 146int(512) 147-- File opened in mode r+t -- 148bool(true) 149int(0) 150-- Testing fseek() with whence = SEEK_SET -- 151int(0) 152int(10) 153bool(false) 154-- Testing fseek() with whence = SEEK_CUR -- 155int(0) 156int(20) 157bool(false) 158-- Testing fseek() with whence = SEEK_END -- 159int(0) 160int(522) 161bool(false) 162int(512) 163-- File opened in mode a -- 164bool(true) 165int(0) 166-- Testing fseek() with whence = SEEK_SET -- 167int(0) 168int(10) 169bool(false) 170-- Testing fseek() with whence = SEEK_CUR -- 171int(0) 172int(20) 173bool(false) 174-- Testing fseek() with whence = SEEK_END -- 175int(0) 176int(522) 177bool(false) 178int(512) 179-- File opened in mode ab -- 180bool(true) 181int(0) 182-- Testing fseek() with whence = SEEK_SET -- 183int(0) 184int(10) 185bool(false) 186-- Testing fseek() with whence = SEEK_CUR -- 187int(0) 188int(20) 189bool(false) 190-- Testing fseek() with whence = SEEK_END -- 191int(0) 192int(522) 193bool(false) 194int(512) 195-- File opened in mode at -- 196bool(true) 197int(0) 198-- Testing fseek() with whence = SEEK_SET -- 199int(0) 200int(10) 201bool(false) 202-- Testing fseek() with whence = SEEK_CUR -- 203int(0) 204int(20) 205bool(false) 206-- Testing fseek() with whence = SEEK_END -- 207int(0) 208int(522) 209bool(false) 210int(512) 211-- File opened in mode a+ -- 212bool(true) 213int(0) 214-- Testing fseek() with whence = SEEK_SET -- 215int(0) 216int(10) 217bool(false) 218-- Testing fseek() with whence = SEEK_CUR -- 219int(0) 220int(20) 221bool(false) 222-- Testing fseek() with whence = SEEK_END -- 223int(0) 224int(522) 225bool(false) 226int(512) 227-- File opened in mode a+b -- 228bool(true) 229int(0) 230-- Testing fseek() with whence = SEEK_SET -- 231int(0) 232int(10) 233bool(false) 234-- Testing fseek() with whence = SEEK_CUR -- 235int(0) 236int(20) 237bool(false) 238-- Testing fseek() with whence = SEEK_END -- 239int(0) 240int(522) 241bool(false) 242int(512) 243-- File opened in mode a+t -- 244bool(true) 245int(0) 246-- Testing fseek() with whence = SEEK_SET -- 247int(0) 248int(10) 249bool(false) 250-- Testing fseek() with whence = SEEK_CUR -- 251int(0) 252int(20) 253bool(false) 254-- Testing fseek() with whence = SEEK_END -- 255int(0) 256int(522) 257bool(false) 258int(512) 259 260-- File having data of type alphanumeric -- 261-- File opened in mode r -- 262bool(true) 263int(0) 264-- Testing fseek() with whence = SEEK_SET -- 265int(0) 266int(10) 267bool(false) 268-- Testing fseek() with whence = SEEK_CUR -- 269int(0) 270int(20) 271bool(false) 272-- Testing fseek() with whence = SEEK_END -- 273int(0) 274int(522) 275bool(false) 276int(512) 277-- File opened in mode rb -- 278bool(true) 279int(0) 280-- Testing fseek() with whence = SEEK_SET -- 281int(0) 282int(10) 283bool(false) 284-- Testing fseek() with whence = SEEK_CUR -- 285int(0) 286int(20) 287bool(false) 288-- Testing fseek() with whence = SEEK_END -- 289int(0) 290int(522) 291bool(false) 292int(512) 293-- File opened in mode rt -- 294bool(true) 295int(0) 296-- Testing fseek() with whence = SEEK_SET -- 297int(0) 298int(10) 299bool(false) 300-- Testing fseek() with whence = SEEK_CUR -- 301int(0) 302int(20) 303bool(false) 304-- Testing fseek() with whence = SEEK_END -- 305int(0) 306int(522) 307bool(false) 308int(512) 309-- File opened in mode r+ -- 310bool(true) 311int(0) 312-- Testing fseek() with whence = SEEK_SET -- 313int(0) 314int(10) 315bool(false) 316-- Testing fseek() with whence = SEEK_CUR -- 317int(0) 318int(20) 319bool(false) 320-- Testing fseek() with whence = SEEK_END -- 321int(0) 322int(522) 323bool(false) 324int(512) 325-- File opened in mode r+b -- 326bool(true) 327int(0) 328-- Testing fseek() with whence = SEEK_SET -- 329int(0) 330int(10) 331bool(false) 332-- Testing fseek() with whence = SEEK_CUR -- 333int(0) 334int(20) 335bool(false) 336-- Testing fseek() with whence = SEEK_END -- 337int(0) 338int(522) 339bool(false) 340int(512) 341-- File opened in mode r+t -- 342bool(true) 343int(0) 344-- Testing fseek() with whence = SEEK_SET -- 345int(0) 346int(10) 347bool(false) 348-- Testing fseek() with whence = SEEK_CUR -- 349int(0) 350int(20) 351bool(false) 352-- Testing fseek() with whence = SEEK_END -- 353int(0) 354int(522) 355bool(false) 356int(512) 357-- File opened in mode a -- 358bool(true) 359int(0) 360-- Testing fseek() with whence = SEEK_SET -- 361int(0) 362int(10) 363bool(false) 364-- Testing fseek() with whence = SEEK_CUR -- 365int(0) 366int(20) 367bool(false) 368-- Testing fseek() with whence = SEEK_END -- 369int(0) 370int(522) 371bool(false) 372int(512) 373-- File opened in mode ab -- 374bool(true) 375int(0) 376-- Testing fseek() with whence = SEEK_SET -- 377int(0) 378int(10) 379bool(false) 380-- Testing fseek() with whence = SEEK_CUR -- 381int(0) 382int(20) 383bool(false) 384-- Testing fseek() with whence = SEEK_END -- 385int(0) 386int(522) 387bool(false) 388int(512) 389-- File opened in mode at -- 390bool(true) 391int(0) 392-- Testing fseek() with whence = SEEK_SET -- 393int(0) 394int(10) 395bool(false) 396-- Testing fseek() with whence = SEEK_CUR -- 397int(0) 398int(20) 399bool(false) 400-- Testing fseek() with whence = SEEK_END -- 401int(0) 402int(522) 403bool(false) 404int(512) 405-- File opened in mode a+ -- 406bool(true) 407int(0) 408-- Testing fseek() with whence = SEEK_SET -- 409int(0) 410int(10) 411bool(false) 412-- Testing fseek() with whence = SEEK_CUR -- 413int(0) 414int(20) 415bool(false) 416-- Testing fseek() with whence = SEEK_END -- 417int(0) 418int(522) 419bool(false) 420int(512) 421-- File opened in mode a+b -- 422bool(true) 423int(0) 424-- Testing fseek() with whence = SEEK_SET -- 425int(0) 426int(10) 427bool(false) 428-- Testing fseek() with whence = SEEK_CUR -- 429int(0) 430int(20) 431bool(false) 432-- Testing fseek() with whence = SEEK_END -- 433int(0) 434int(522) 435bool(false) 436int(512) 437-- File opened in mode a+t -- 438bool(true) 439int(0) 440-- Testing fseek() with whence = SEEK_SET -- 441int(0) 442int(10) 443bool(false) 444-- Testing fseek() with whence = SEEK_CUR -- 445int(0) 446int(20) 447bool(false) 448-- Testing fseek() with whence = SEEK_END -- 449int(0) 450int(522) 451bool(false) 452int(512) 453Done 454