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