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