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