1--TEST-- 2Test fwrite() function : usage variations - a, ab, at, a+, a+b & a+t modes 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 20echo "*** Testing fwrite() various operations ***\n"; 21 22// include the file.inc for Function: function delete_file($filename) 23include ("file.inc"); 24 25/* 26 Test fwrite with file opened in mode : a,ab,at,a+,a+b,a+t 27 File having content of type numeric, text,text_with_new_line & alphanumeric 28*/ 29 30$file_modes = array("a","ab","at","a+","a+b","a+t"); 31$file_content_types = array("numeric","text","text_with_new_line","alphanumeric"); 32 33 34foreach($file_content_types as $file_content_type) { 35 echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n"; 36 37 /* open the file using $files_modes and perform fwrite() on it */ 38 foreach($file_modes as $file_mode) { 39 echo "-- Opening file in $file_mode --\n"; 40 41 // create temp file and fill it content of type $file_content_type 42 $filename = dirname(__FILE__)."/fwrite_variation3.tmp"; // this is name of the file 43 create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 3); 44 45 $file_handle = fopen($filename, $file_mode); 46 if(!$file_handle) { 47 echo "Error: failed to fopen() file: $filename!"; 48 exit(); 49 } 50 51 $data_to_be_written=""; 52 fill_buffer($data_to_be_written,$file_content_type,1024); //get the data of size 1024 53 54 /* Write the data into the file, verify it by checking the file pointer position, eof position, 55 filesize & by displaying the content */ 56 57 // append the data to the file, starting from current position of the file pointer 58 var_dump( ftell($file_handle) ); // expected: 1024 59 var_dump( fwrite($file_handle,$data_to_be_written,400) ); 60 var_dump( ftell($file_handle) ); // expected: 1024 + 400 61 var_dump( feof($file_handle) ); // expected : true 62 63 /*overwrite data in middle of the file*/ 64 fseek($file_handle, SEEK_SET, (1024 + 400)/2 ); 65 var_dump( ftell($file_handle)); // expected: (1024 + 400)/2 66 var_dump( fwrite($file_handle, $data_to_be_written, 200) ); 67 var_dump( ftell($file_handle) ); 68 var_dump( feof($file_handle) ); //Expecting bool(false) 69 70 /* check the filesize and display file content */ 71 // close the file, get the size and content of the file. 72 var_dump( fclose($file_handle) ); 73 clearstatcache();//clears file status cache 74 var_dump( filesize($filename) ); 75 var_dump(md5(file_get_contents($filename))); 76 // delete the file created 77 delete_file($filename); // delete file with name fwrite_variation3.tmp 78 } // end of inner foreach loop 79} // end of outer foreach loop 80 81echo "Done\n"; 82?> 83--EXPECT-- 84*** Testing fwrite() various operations *** 85 86-- Testing fwrite() with file having content of type numeric -- 87-- Opening file in a -- 88int(0) 89int(400) 90int(400) 91bool(false) 92int(400) 93int(200) 94int(600) 95bool(false) 96bool(true) 97int(1624) 98string(32) "59ce5bf03b69069d00d6354bdc969ff6" 99-- Opening file in ab -- 100int(0) 101int(400) 102int(400) 103bool(false) 104int(400) 105int(200) 106int(600) 107bool(false) 108bool(true) 109int(1624) 110string(32) "59ce5bf03b69069d00d6354bdc969ff6" 111-- Opening file in at -- 112int(0) 113int(400) 114int(400) 115bool(false) 116int(400) 117int(200) 118int(600) 119bool(false) 120bool(true) 121int(1624) 122string(32) "59ce5bf03b69069d00d6354bdc969ff6" 123-- Opening file in a+ -- 124int(0) 125int(400) 126int(400) 127bool(false) 128int(400) 129int(200) 130int(600) 131bool(false) 132bool(true) 133int(1624) 134string(32) "59ce5bf03b69069d00d6354bdc969ff6" 135-- Opening file in a+b -- 136int(0) 137int(400) 138int(400) 139bool(false) 140int(400) 141int(200) 142int(600) 143bool(false) 144bool(true) 145int(1624) 146string(32) "59ce5bf03b69069d00d6354bdc969ff6" 147-- Opening file in a+t -- 148int(0) 149int(400) 150int(400) 151bool(false) 152int(400) 153int(200) 154int(600) 155bool(false) 156bool(true) 157int(1624) 158string(32) "59ce5bf03b69069d00d6354bdc969ff6" 159 160-- Testing fwrite() with file having content of type text -- 161-- Opening file in a -- 162int(0) 163int(400) 164int(400) 165bool(false) 166int(400) 167int(200) 168int(600) 169bool(false) 170bool(true) 171int(1624) 172string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 173-- Opening file in ab -- 174int(0) 175int(400) 176int(400) 177bool(false) 178int(400) 179int(200) 180int(600) 181bool(false) 182bool(true) 183int(1624) 184string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 185-- Opening file in at -- 186int(0) 187int(400) 188int(400) 189bool(false) 190int(400) 191int(200) 192int(600) 193bool(false) 194bool(true) 195int(1624) 196string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 197-- Opening file in a+ -- 198int(0) 199int(400) 200int(400) 201bool(false) 202int(400) 203int(200) 204int(600) 205bool(false) 206bool(true) 207int(1624) 208string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 209-- Opening file in a+b -- 210int(0) 211int(400) 212int(400) 213bool(false) 214int(400) 215int(200) 216int(600) 217bool(false) 218bool(true) 219int(1624) 220string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 221-- Opening file in a+t -- 222int(0) 223int(400) 224int(400) 225bool(false) 226int(400) 227int(200) 228int(600) 229bool(false) 230bool(true) 231int(1624) 232string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 233 234-- Testing fwrite() with file having content of type text_with_new_line -- 235-- Opening file in a -- 236int(0) 237int(400) 238int(400) 239bool(false) 240int(400) 241int(200) 242int(600) 243bool(false) 244bool(true) 245int(1624) 246string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 247-- Opening file in ab -- 248int(0) 249int(400) 250int(400) 251bool(false) 252int(400) 253int(200) 254int(600) 255bool(false) 256bool(true) 257int(1624) 258string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 259-- Opening file in at -- 260int(0) 261int(400) 262int(400) 263bool(false) 264int(400) 265int(200) 266int(600) 267bool(false) 268bool(true) 269int(1624) 270string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 271-- Opening file in a+ -- 272int(0) 273int(400) 274int(400) 275bool(false) 276int(400) 277int(200) 278int(600) 279bool(false) 280bool(true) 281int(1624) 282string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 283-- Opening file in a+b -- 284int(0) 285int(400) 286int(400) 287bool(false) 288int(400) 289int(200) 290int(600) 291bool(false) 292bool(true) 293int(1624) 294string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 295-- Opening file in a+t -- 296int(0) 297int(400) 298int(400) 299bool(false) 300int(400) 301int(200) 302int(600) 303bool(false) 304bool(true) 305int(1624) 306string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 307 308-- Testing fwrite() with file having content of type alphanumeric -- 309-- Opening file in a -- 310int(0) 311int(400) 312int(400) 313bool(false) 314int(400) 315int(200) 316int(600) 317bool(false) 318bool(true) 319int(1624) 320string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 321-- Opening file in ab -- 322int(0) 323int(400) 324int(400) 325bool(false) 326int(400) 327int(200) 328int(600) 329bool(false) 330bool(true) 331int(1624) 332string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 333-- Opening file in at -- 334int(0) 335int(400) 336int(400) 337bool(false) 338int(400) 339int(200) 340int(600) 341bool(false) 342bool(true) 343int(1624) 344string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 345-- Opening file in a+ -- 346int(0) 347int(400) 348int(400) 349bool(false) 350int(400) 351int(200) 352int(600) 353bool(false) 354bool(true) 355int(1624) 356string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 357-- Opening file in a+b -- 358int(0) 359int(400) 360int(400) 361bool(false) 362int(400) 363int(200) 364int(600) 365bool(false) 366bool(true) 367int(1624) 368string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 369-- Opening file in a+t -- 370int(0) 371int(400) 372int(400) 373bool(false) 374int(400) 375int(200) 376int(600) 377bool(false) 378bool(true) 379int(1624) 380string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 381Done 382