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