1--TEST-- 2Test fwrite() function : usage variations - r+, r+b & r+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 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 : r+,r+b,r+t 18 File having content of type numeric, text,text_with_new_line & alphanumeric 19*/ 20 21$file_modes = array("r+", "r+b", "r+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 the data of type $file_content_type 33 $filename = __DIR__."/fwrite_variation2.tmp"; // this is name of the file 34 create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 2); 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 48 /*overwrite first 400 bytes in the file*/ 49 var_dump( ftell($file_handle) ); // expected : 0 50 var_dump( fwrite($file_handle, $data_to_be_written, 400)); 51 var_dump( ftell($file_handle) ); // expected: 400 52 var_dump( feof($file_handle) ); //Expecting bool(false) 53 54 /*overwrite data in middle of the file*/ 55 fseek($file_handle, SEEK_SET, 1024/2 ); 56 var_dump( ftell($file_handle)); // expected: 1024/2 57 var_dump( fwrite($file_handle, $data_to_be_written, 200) ); 58 var_dump( ftell($file_handle) ); 59 var_dump( feof($file_handle) ); //Expecting bool(false) 60 61 /* write at the end of the file */ 62 fseek($file_handle, SEEK_END, 0); 63 var_dump( ftell($file_handle) ); // expected: 1024 64 var_dump( feof($file_handle) ); 65 var_dump( fwrite($file_handle, $data_to_be_written, 200) ); 66 var_dump( ftell($file_handle) ); 67 var_dump( feof($file_handle) ); //Expecting bool(false) 68 69 /* display the file content, check the file size */ 70 var_dump( fclose($file_handle) ); 71 clearstatcache();//clears file status cache 72 var_dump( filesize($filename) ); 73 var_dump(md5(file_get_contents($filename))); 74 delete_file($filename); // delete file with name fwrite_variation2.tmp 75 } // end of inner foreach loop 76} // end of outer foreach loop 77 78echo "Done\n"; 79?> 80--EXPECT-- 81*** Testing fwrite() various operations *** 82 83-- Testing fwrite() with file having content of type numeric -- 84-- Opening file in r+ -- 85int(0) 86int(400) 87int(400) 88bool(false) 89int(400) 90int(200) 91int(600) 92bool(false) 93int(2) 94bool(false) 95int(200) 96int(202) 97bool(false) 98bool(true) 99int(1024) 100string(32) "950b7457d1deb6332f2fc5d42f3129d6" 101-- Opening file in r+b -- 102int(0) 103int(400) 104int(400) 105bool(false) 106int(400) 107int(200) 108int(600) 109bool(false) 110int(2) 111bool(false) 112int(200) 113int(202) 114bool(false) 115bool(true) 116int(1024) 117string(32) "950b7457d1deb6332f2fc5d42f3129d6" 118-- Opening file in r+t -- 119int(0) 120int(400) 121int(400) 122bool(false) 123int(400) 124int(200) 125int(600) 126bool(false) 127int(2) 128bool(false) 129int(200) 130int(202) 131bool(false) 132bool(true) 133int(1024) 134string(32) "950b7457d1deb6332f2fc5d42f3129d6" 135 136-- Testing fwrite() with file having content of type text -- 137-- Opening file in r+ -- 138int(0) 139int(400) 140int(400) 141bool(false) 142int(400) 143int(200) 144int(600) 145bool(false) 146int(2) 147bool(false) 148int(200) 149int(202) 150bool(false) 151bool(true) 152int(1024) 153string(32) "3bdaf80dae28bc24bb304daa5ffee16c" 154-- Opening file in r+b -- 155int(0) 156int(400) 157int(400) 158bool(false) 159int(400) 160int(200) 161int(600) 162bool(false) 163int(2) 164bool(false) 165int(200) 166int(202) 167bool(false) 168bool(true) 169int(1024) 170string(32) "3bdaf80dae28bc24bb304daa5ffee16c" 171-- Opening file in r+t -- 172int(0) 173int(400) 174int(400) 175bool(false) 176int(400) 177int(200) 178int(600) 179bool(false) 180int(2) 181bool(false) 182int(200) 183int(202) 184bool(false) 185bool(true) 186int(1024) 187string(32) "3bdaf80dae28bc24bb304daa5ffee16c" 188 189-- Testing fwrite() with file having content of type text_with_new_line -- 190-- Opening file in r+ -- 191int(0) 192int(400) 193int(400) 194bool(false) 195int(400) 196int(200) 197int(600) 198bool(false) 199int(2) 200bool(false) 201int(200) 202int(202) 203bool(false) 204bool(true) 205int(1024) 206string(32) "b188d7c8aa229cbef067e5970f2daba9" 207-- Opening file in r+b -- 208int(0) 209int(400) 210int(400) 211bool(false) 212int(400) 213int(200) 214int(600) 215bool(false) 216int(2) 217bool(false) 218int(200) 219int(202) 220bool(false) 221bool(true) 222int(1024) 223string(32) "b188d7c8aa229cbef067e5970f2daba9" 224-- Opening file in r+t -- 225int(0) 226int(400) 227int(400) 228bool(false) 229int(400) 230int(200) 231int(600) 232bool(false) 233int(2) 234bool(false) 235int(200) 236int(202) 237bool(false) 238bool(true) 239int(1024) 240string(32) "b188d7c8aa229cbef067e5970f2daba9" 241 242-- Testing fwrite() with file having content of type alphanumeric -- 243-- Opening file in r+ -- 244int(0) 245int(400) 246int(400) 247bool(false) 248int(400) 249int(200) 250int(600) 251bool(false) 252int(2) 253bool(false) 254int(200) 255int(202) 256bool(false) 257bool(true) 258int(1024) 259string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9" 260-- Opening file in r+b -- 261int(0) 262int(400) 263int(400) 264bool(false) 265int(400) 266int(200) 267int(600) 268bool(false) 269int(2) 270bool(false) 271int(200) 272int(202) 273bool(false) 274bool(true) 275int(1024) 276string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9" 277-- Opening file in r+t -- 278int(0) 279int(400) 280int(400) 281bool(false) 282int(400) 283int(200) 284int(600) 285bool(false) 286int(2) 287bool(false) 288int(200) 289int(202) 290bool(false) 291bool(true) 292int(1024) 293string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9" 294Done 295