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 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 : 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 76 } // end of inner foreach loop 77} // end of outer foreach loop 78 79echo "Done\n"; 80?> 81--EXPECT-- 82*** Testing fwrite() various operations *** 83 84-- Testing fwrite() with file having content of type numeric -- 85-- Opening file in r+ -- 86int(0) 87int(400) 88int(400) 89bool(false) 90int(400) 91int(200) 92int(600) 93bool(false) 94int(2) 95bool(false) 96int(200) 97int(202) 98bool(false) 99bool(true) 100int(1024) 101string(32) "950b7457d1deb6332f2fc5d42f3129d6" 102-- Opening file in r+b -- 103int(0) 104int(400) 105int(400) 106bool(false) 107int(400) 108int(200) 109int(600) 110bool(false) 111int(2) 112bool(false) 113int(200) 114int(202) 115bool(false) 116bool(true) 117int(1024) 118string(32) "950b7457d1deb6332f2fc5d42f3129d6" 119-- Opening file in r+t -- 120int(0) 121int(400) 122int(400) 123bool(false) 124int(400) 125int(200) 126int(600) 127bool(false) 128int(2) 129bool(false) 130int(200) 131int(202) 132bool(false) 133bool(true) 134int(1024) 135string(32) "950b7457d1deb6332f2fc5d42f3129d6" 136 137-- Testing fwrite() with file having content of type text -- 138-- Opening file in r+ -- 139int(0) 140int(400) 141int(400) 142bool(false) 143int(400) 144int(200) 145int(600) 146bool(false) 147int(2) 148bool(false) 149int(200) 150int(202) 151bool(false) 152bool(true) 153int(1024) 154string(32) "3bdaf80dae28bc24bb304daa5ffee16c" 155-- Opening file in r+b -- 156int(0) 157int(400) 158int(400) 159bool(false) 160int(400) 161int(200) 162int(600) 163bool(false) 164int(2) 165bool(false) 166int(200) 167int(202) 168bool(false) 169bool(true) 170int(1024) 171string(32) "3bdaf80dae28bc24bb304daa5ffee16c" 172-- Opening file in r+t -- 173int(0) 174int(400) 175int(400) 176bool(false) 177int(400) 178int(200) 179int(600) 180bool(false) 181int(2) 182bool(false) 183int(200) 184int(202) 185bool(false) 186bool(true) 187int(1024) 188string(32) "3bdaf80dae28bc24bb304daa5ffee16c" 189 190-- Testing fwrite() with file having content of type text_with_new_line -- 191-- Opening file in r+ -- 192int(0) 193int(400) 194int(400) 195bool(false) 196int(400) 197int(200) 198int(600) 199bool(false) 200int(2) 201bool(false) 202int(200) 203int(202) 204bool(false) 205bool(true) 206int(1024) 207string(32) "b188d7c8aa229cbef067e5970f2daba9" 208-- Opening file in r+b -- 209int(0) 210int(400) 211int(400) 212bool(false) 213int(400) 214int(200) 215int(600) 216bool(false) 217int(2) 218bool(false) 219int(200) 220int(202) 221bool(false) 222bool(true) 223int(1024) 224string(32) "b188d7c8aa229cbef067e5970f2daba9" 225-- Opening file in r+t -- 226int(0) 227int(400) 228int(400) 229bool(false) 230int(400) 231int(200) 232int(600) 233bool(false) 234int(2) 235bool(false) 236int(200) 237int(202) 238bool(false) 239bool(true) 240int(1024) 241string(32) "991652c76db8d17c790c702ac0a6dc5f" 242 243-- Testing fwrite() with file having content of type alphanumeric -- 244-- Opening file in r+ -- 245int(0) 246int(400) 247int(400) 248bool(false) 249int(400) 250int(200) 251int(600) 252bool(false) 253int(2) 254bool(false) 255int(200) 256int(202) 257bool(false) 258bool(true) 259int(1024) 260string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9" 261-- Opening file in r+b -- 262int(0) 263int(400) 264int(400) 265bool(false) 266int(400) 267int(200) 268int(600) 269bool(false) 270int(2) 271bool(false) 272int(200) 273int(202) 274bool(false) 275bool(true) 276int(1024) 277string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9" 278-- Opening file in r+t -- 279int(0) 280int(400) 281int(400) 282bool(false) 283int(400) 284int(200) 285int(600) 286bool(false) 287int(2) 288bool(false) 289int(200) 290int(202) 291bool(false) 292bool(true) 293int(1024) 294string(32) "5d4ec23a3d9dd447e2f702d9e0e114d9" 295Done 296