1--TEST-- 2Test fwrite() function : usage variations - r, rb & rt modes 3--FILE-- 4<?php 5/* 6 Prototype: int fwrite ( resource $handle,string string, [, int $length] ); 7 Description: fwrite() writes the contents of string to the file stream pointed to by handle. 8 If the length arquement is given,writing will stop after length bytes have been 9 written or the end of string reached, whichever comes first. 10 fwrite() returns the number of bytes written or FALSE on error 11*/ 12 13 14echo "*** Testing fwrite() various operations ***\n"; 15 16// include the file.inc for Function: function delete_file($filename) 17include ("file.inc"); 18 19/* 20 Test fwrite with file opened in mode : r,rb,rt 21 File having content of type numeric, text,text_with_new_line & alphanumeric 22*/ 23 24$file_modes = array("r","rb","rt"); 25$file_content_types = array("numeric","text","text_with_new_line","alphanumeric"); 26 27foreach($file_content_types as $file_content_type) { 28 echo "\n-- Testing fwrite() with file having content of type ". $file_content_type ." --\n"; 29 30 /* open the file using $files_modes and perform fwrite() on it */ 31 foreach($file_modes as $file_mode) { 32 echo "-- Opening file in $file_mode --\n"; 33 34 // create the temp file with content of type $file_content_type 35 $filename = __DIR__."/fwrite_variation1.tmp"; // this is name of the file 36 create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fwrite_variation"); 37 38 $file_handle = fopen($filename, $file_mode); 39 if(!$file_handle) { 40 echo "Error: failed to fopen() file: $filename!"; 41 exit(); 42 } 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 into the file, verify it by checking the file pointer position, eof position, 48 filesize & by displaying the content */ 49 50 var_dump( ftell($file_handle) ); // expected: 0 51 var_dump( fwrite($file_handle, $data_to_be_written )); 52 var_dump( ftell($file_handle) ); // expected: 0 53 var_dump( feof($file_handle) ); // expected: false 54 55 // move the file pointer to end of the file and try fwrite() 56 fseek($file_handle, SEEK_END, 0); 57 var_dump( ftell($file_handle) ); // expecting 1024 58 var_dump( fwrite($file_handle, $data_to_be_written) ); // fwrite to fail 59 var_dump( ftell($file_handle) ); //check that file pointer points at eof, expected: 1024 60 var_dump( feof($file_handle) ); // ensure that feof() points to eof, expected: true 61 62 // ensure that file content/size didn't change. 63 var_dump( fclose($file_handle) ); 64 clearstatcache();//clears file status cache 65 var_dump( filesize($filename) ); // expected: 1024 66 var_dump(md5(file_get_contents($filename))); // hash the output 67 delete_file($filename); // delete file with name fwrite_variation1.tmp 68 } // end of inner foreach loop 69} // end of outer foreach loop 70 71echo "Done\n"; 72?> 73--EXPECTF-- 74*** Testing fwrite() various operations *** 75 76-- Testing fwrite() with file having content of type numeric -- 77-- Opening file in r -- 78int(0) 79 80Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 81bool(false) 82int(0) 83bool(false) 84int(2) 85 86Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 87bool(false) 88int(2) 89bool(false) 90bool(true) 91int(1024) 92string(32) "950b7457d1deb6332f2fc5d42f3129d6" 93-- Opening file in rb -- 94int(0) 95 96Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 97bool(false) 98int(0) 99bool(false) 100int(2) 101 102Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 103bool(false) 104int(2) 105bool(false) 106bool(true) 107int(1024) 108string(32) "950b7457d1deb6332f2fc5d42f3129d6" 109-- Opening file in rt -- 110int(0) 111 112Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 113bool(false) 114int(0) 115bool(false) 116int(2) 117 118Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 119bool(false) 120int(2) 121bool(false) 122bool(true) 123int(1024) 124string(32) "950b7457d1deb6332f2fc5d42f3129d6" 125 126-- Testing fwrite() with file having content of type text -- 127-- Opening file in r -- 128int(0) 129 130Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 131bool(false) 132int(0) 133bool(false) 134int(2) 135 136Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 137bool(false) 138int(2) 139bool(false) 140bool(true) 141int(1024) 142string(32) "e486000c4c8452774f746a27658d87fa" 143-- Opening file in rb -- 144int(0) 145 146Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 147bool(false) 148int(0) 149bool(false) 150int(2) 151 152Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 153bool(false) 154int(2) 155bool(false) 156bool(true) 157int(1024) 158string(32) "e486000c4c8452774f746a27658d87fa" 159-- Opening file in rt -- 160int(0) 161 162Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 163bool(false) 164int(0) 165bool(false) 166int(2) 167 168Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 169bool(false) 170int(2) 171bool(false) 172bool(true) 173int(1024) 174string(32) "e486000c4c8452774f746a27658d87fa" 175 176-- Testing fwrite() with file having content of type text_with_new_line -- 177-- Opening file in r -- 178int(0) 179 180Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 181bool(false) 182int(0) 183bool(false) 184int(2) 185 186Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 187bool(false) 188int(2) 189bool(false) 190bool(true) 191int(1024) 192string(32) "b09c8026a64a88d36d4c2f17983964bb" 193-- Opening file in rb -- 194int(0) 195 196Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 197bool(false) 198int(0) 199bool(false) 200int(2) 201 202Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 203bool(false) 204int(2) 205bool(false) 206bool(true) 207int(1024) 208string(32) "b09c8026a64a88d36d4c2f17983964bb" 209-- Opening file in rt -- 210int(0) 211 212Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 213bool(false) 214int(0) 215bool(false) 216int(2) 217 218Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 219bool(false) 220int(2) 221bool(false) 222bool(true) 223int(1024) 224string(32) "b09c8026a64a88d36d4c2f17983964bb" 225 226-- Testing fwrite() with file having content of type alphanumeric -- 227-- Opening file in r -- 228int(0) 229 230Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 231bool(false) 232int(0) 233bool(false) 234int(2) 235 236Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 237bool(false) 238int(2) 239bool(false) 240bool(true) 241int(1024) 242string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 243-- Opening file in rb -- 244int(0) 245 246Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 247bool(false) 248int(0) 249bool(false) 250int(2) 251 252Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 253bool(false) 254int(2) 255bool(false) 256bool(true) 257int(1024) 258string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 259-- Opening file in rt -- 260int(0) 261 262Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 263bool(false) 264int(0) 265bool(false) 266int(2) 267 268Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 269bool(false) 270int(2) 271bool(false) 272bool(true) 273int(1024) 274string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 275Done 276