1--TEST-- 2Test fwrite() function : basic functionality 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: int fwrite ( resource $handle,string string, [, int $length] ); 13 Description: fwrite() writes the contents of string to the file stream pointed to by handle. 14 If the length arquement is given,writing will stop after length bytes have been 15 written or the end of string reached, whichever comes first. 16 fwrite() returns the number of bytes written or FALSE on error 17*/ 18 19// include the file.inc for Function: function delete_file($filename) 20include ("file.inc"); 21 22echo "*** Testing fwrite() basic operations ***\n"; 23/* 24 test fwrite with file opened in mode : w,wb,wt,w+,w+b,w+t 25 File containing data of type, numeric, text, text_with_new_line, alphanumeric 26*/ 27$file_modes = array( "w", "wb", "wt", "w+", "w+b", "w+t"); 28$file_content_types = array("numeric","text","text_with_new_line","alphanumeric"); 29 30foreach($file_content_types as $file_content_type) { 31 echo "\n-- Testing fwrite() with file having data of type ". $file_content_type ." --\n"; 32 $filename = dirname(__FILE__)."/fwrite_basic.tmp"; // this is name of the file 33 34 for($inner_loop_counter = 0; 35 $inner_loop_counter < count($file_modes); 36 $inner_loop_counter++) { 37 echo "-- File opened in mode : " . $file_modes[$inner_loop_counter]. " --\n"; 38 /* open the file using $files_modes and perform fwrite() on it */ 39 $file_handle = fopen($filename, $file_modes[$inner_loop_counter]); 40 if (!$file_handle) { 41 echo "Error: failed to fopen() file: $filename!"; 42 exit(); 43 } 44 $data_to_be_written=""; 45 fill_buffer($data_to_be_written, $file_content_type, 1024); //get the data of size 1024 46 47 /* Write the data in to the file, verify the write by checking file pointer position, 48 eof position, and data. */ 49 // writing 100 bytes 50 var_dump( ftell($file_handle) ); // Expecting 0 51 var_dump( fwrite($file_handle, $data_to_be_written, 100)); //int(100) 52 var_dump( feof($file_handle) ); // expected : false 53 var_dump( ftell($file_handle) ); //expected: 100 54 55 // trying to write more than the available data, available 1024 bytes but trying 2048 56 var_dump( fwrite($file_handle, $data_to_be_written, 2048)); //int(1024) 57 var_dump( feof($file_handle) ); // expected : false 58 var_dump( ftell($file_handle) ); // expected: 1124 59 60 // fwrite() without length parameter 61 var_dump( fwrite($file_handle, $data_to_be_written)); //int(1024) 62 var_dump( ftell($file_handle) ); // expected: 2148 63 var_dump( feof($file_handle) ); // expected: false 64 65 // close the file, get the size and content of the file. 66 var_dump( fclose($file_handle) ); //expected : true 67 clearstatcache();//clears file status cache 68 var_dump( filesize($filename) ); // expected: 2148 69 var_dump(md5(file_get_contents($filename))); // hash the output 70 71 } // end of inner for loop 72 73 // delete the file created : fwrite_basic.tmp 74 delete_file($filename); 75} // end of outer foreach loop 76echo "Done\n"; 77?> 78--EXPECTF-- 79*** Testing fwrite() basic operations *** 80 81-- Testing fwrite() with file having data of type numeric -- 82-- File opened in mode : w -- 83int(0) 84int(100) 85bool(false) 86int(100) 87int(1024) 88bool(false) 89int(1124) 90int(1024) 91int(2148) 92bool(false) 93bool(true) 94int(2148) 95string(32) "04db34906fe2c56dcfbd649b7d916974" 96-- File opened in mode : wb -- 97int(0) 98int(100) 99bool(false) 100int(100) 101int(1024) 102bool(false) 103int(1124) 104int(1024) 105int(2148) 106bool(false) 107bool(true) 108int(2148) 109string(32) "04db34906fe2c56dcfbd649b7d916974" 110-- File opened in mode : wt -- 111int(0) 112int(100) 113bool(false) 114int(100) 115int(1024) 116bool(false) 117int(1124) 118int(1024) 119int(2148) 120bool(false) 121bool(true) 122int(2148) 123string(32) "04db34906fe2c56dcfbd649b7d916974" 124-- File opened in mode : w+ -- 125int(0) 126int(100) 127bool(false) 128int(100) 129int(1024) 130bool(false) 131int(1124) 132int(1024) 133int(2148) 134bool(false) 135bool(true) 136int(2148) 137string(32) "04db34906fe2c56dcfbd649b7d916974" 138-- File opened in mode : w+b -- 139int(0) 140int(100) 141bool(false) 142int(100) 143int(1024) 144bool(false) 145int(1124) 146int(1024) 147int(2148) 148bool(false) 149bool(true) 150int(2148) 151string(32) "04db34906fe2c56dcfbd649b7d916974" 152-- File opened in mode : w+t -- 153int(0) 154int(100) 155bool(false) 156int(100) 157int(1024) 158bool(false) 159int(1124) 160int(1024) 161int(2148) 162bool(false) 163bool(true) 164int(2148) 165string(32) "04db34906fe2c56dcfbd649b7d916974" 166 167-- Testing fwrite() with file having data of type text -- 168-- File opened in mode : w -- 169int(0) 170int(100) 171bool(false) 172int(100) 173int(1024) 174bool(false) 175int(1124) 176int(1024) 177int(2148) 178bool(false) 179bool(true) 180int(2148) 181string(32) "9c08ac77b7a93a84dd0b055900165e84" 182-- File opened in mode : wb -- 183int(0) 184int(100) 185bool(false) 186int(100) 187int(1024) 188bool(false) 189int(1124) 190int(1024) 191int(2148) 192bool(false) 193bool(true) 194int(2148) 195string(32) "9c08ac77b7a93a84dd0b055900165e84" 196-- File opened in mode : wt -- 197int(0) 198int(100) 199bool(false) 200int(100) 201int(1024) 202bool(false) 203int(1124) 204int(1024) 205int(2148) 206bool(false) 207bool(true) 208int(2148) 209string(32) "9c08ac77b7a93a84dd0b055900165e84" 210-- File opened in mode : w+ -- 211int(0) 212int(100) 213bool(false) 214int(100) 215int(1024) 216bool(false) 217int(1124) 218int(1024) 219int(2148) 220bool(false) 221bool(true) 222int(2148) 223string(32) "9c08ac77b7a93a84dd0b055900165e84" 224-- File opened in mode : w+b -- 225int(0) 226int(100) 227bool(false) 228int(100) 229int(1024) 230bool(false) 231int(1124) 232int(1024) 233int(2148) 234bool(false) 235bool(true) 236int(2148) 237string(32) "9c08ac77b7a93a84dd0b055900165e84" 238-- File opened in mode : w+t -- 239int(0) 240int(100) 241bool(false) 242int(100) 243int(1024) 244bool(false) 245int(1124) 246int(1024) 247int(2148) 248bool(false) 249bool(true) 250int(2148) 251string(32) "9c08ac77b7a93a84dd0b055900165e84" 252 253-- Testing fwrite() with file having data of type text_with_new_line -- 254-- File opened in mode : w -- 255int(0) 256int(100) 257bool(false) 258int(100) 259int(1024) 260bool(false) 261int(1124) 262int(1024) 263int(2148) 264bool(false) 265bool(true) 266int(2148) 267string(32) "56a1963cc292d7f8245219116d9eca40" 268-- File opened in mode : wb -- 269int(0) 270int(100) 271bool(false) 272int(100) 273int(1024) 274bool(false) 275int(1124) 276int(1024) 277int(2148) 278bool(false) 279bool(true) 280int(2148) 281string(32) "56a1963cc292d7f8245219116d9eca40" 282-- File opened in mode : wt -- 283int(0) 284int(100) 285bool(false) 286int(100) 287int(1024) 288bool(false) 289int(1124) 290int(1024) 291int(2148) 292bool(false) 293bool(true) 294int(2148) 295string(32) "56a1963cc292d7f8245219116d9eca40" 296-- File opened in mode : w+ -- 297int(0) 298int(100) 299bool(false) 300int(100) 301int(1024) 302bool(false) 303int(1124) 304int(1024) 305int(2148) 306bool(false) 307bool(true) 308int(2148) 309string(32) "56a1963cc292d7f8245219116d9eca40" 310-- File opened in mode : w+b -- 311int(0) 312int(100) 313bool(false) 314int(100) 315int(1024) 316bool(false) 317int(1124) 318int(1024) 319int(2148) 320bool(false) 321bool(true) 322int(2148) 323string(32) "56a1963cc292d7f8245219116d9eca40" 324-- File opened in mode : w+t -- 325int(0) 326int(100) 327bool(false) 328int(100) 329int(1024) 330bool(false) 331int(1124) 332int(1024) 333int(2148) 334bool(false) 335bool(true) 336int(2148) 337string(32) "56a1963cc292d7f8245219116d9eca40" 338 339-- Testing fwrite() with file having data of type alphanumeric -- 340-- File opened in mode : w -- 341int(0) 342int(100) 343bool(false) 344int(100) 345int(1024) 346bool(false) 347int(1124) 348int(1024) 349int(2148) 350bool(false) 351bool(true) 352int(2148) 353string(32) "719e3329c19218c12d232f2ee81e100f" 354-- File opened in mode : wb -- 355int(0) 356int(100) 357bool(false) 358int(100) 359int(1024) 360bool(false) 361int(1124) 362int(1024) 363int(2148) 364bool(false) 365bool(true) 366int(2148) 367string(32) "719e3329c19218c12d232f2ee81e100f" 368-- File opened in mode : wt -- 369int(0) 370int(100) 371bool(false) 372int(100) 373int(1024) 374bool(false) 375int(1124) 376int(1024) 377int(2148) 378bool(false) 379bool(true) 380int(2148) 381string(32) "719e3329c19218c12d232f2ee81e100f" 382-- File opened in mode : w+ -- 383int(0) 384int(100) 385bool(false) 386int(100) 387int(1024) 388bool(false) 389int(1124) 390int(1024) 391int(2148) 392bool(false) 393bool(true) 394int(2148) 395string(32) "719e3329c19218c12d232f2ee81e100f" 396-- File opened in mode : w+b -- 397int(0) 398int(100) 399bool(false) 400int(100) 401int(1024) 402bool(false) 403int(1124) 404int(1024) 405int(2148) 406bool(false) 407bool(true) 408int(2148) 409string(32) "719e3329c19218c12d232f2ee81e100f" 410-- File opened in mode : w+t -- 411int(0) 412int(100) 413bool(false) 414int(100) 415int(1024) 416bool(false) 417int(1124) 418int(1024) 419int(2148) 420bool(false) 421bool(true) 422int(2148) 423string(32) "719e3329c19218c12d232f2ee81e100f" 424Done