1--TEST-- 2Test fwrite() function : usage variations - r, rb & rt 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 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 : r,rb,rt 27 File having content of type numeric, text,text_with_new_line & alphanumeric 28*/ 29 30$file_modes = array("r","rb","rt"); 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 the temp file with content of type $file_content_type 42 $filename = __DIR__."/fwrite_variation1私はガラスを食べられます1.tmp"; // this is name of the file 43 create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fwrite_variation1私はガラスを食べられます"); 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 var_dump( ftell($file_handle) ); // expected: 0 58 var_dump( fwrite($file_handle, $data_to_be_written )); 59 var_dump( ftell($file_handle) ); // expected: 0 60 var_dump( feof($file_handle) ); // expected: false 61 62 // move the file pointer to end of the file and try fwrite() 63 fseek($file_handle, SEEK_END, 0); 64 var_dump( ftell($file_handle) ); // expecting 1024 65 var_dump( fwrite($file_handle, $data_to_be_written) ); // fwrite to fail 66 var_dump( ftell($file_handle) ); //check that file pointer points at eof, expected: 1024 67 var_dump( feof($file_handle) ); // ensure that feof() points to eof, expected: true 68 69 // ensure that file content/size didn't change. 70 var_dump( fclose($file_handle) ); 71 clearstatcache();//clears file status cache 72 var_dump( filesize($filename) ); // expected: 1024 73 var_dump(md5(file_get_contents($filename))); // hash the output 74 delete_file($filename); // delete file with name fwrite_variation1.tmp 75 76 } // end of inner foreach loop 77} // end of outer foreach loop 78 79echo "Done\n"; 80?> 81--EXPECTF-- 82*** Testing fwrite() various operations *** 83 84-- Testing fwrite() with file having content of type numeric -- 85-- Opening file in r -- 86int(0) 87 88Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 89bool(false) 90int(0) 91bool(false) 92int(2) 93 94Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 95bool(false) 96int(2) 97bool(false) 98bool(true) 99int(1024) 100string(32) "950b7457d1deb6332f2fc5d42f3129d6" 101-- Opening file in rb -- 102int(0) 103 104Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 105bool(false) 106int(0) 107bool(false) 108int(2) 109 110Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 111bool(false) 112int(2) 113bool(false) 114bool(true) 115int(1024) 116string(32) "950b7457d1deb6332f2fc5d42f3129d6" 117-- Opening file in rt -- 118int(0) 119 120Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 121bool(false) 122int(0) 123bool(false) 124int(2) 125 126Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 127bool(false) 128int(2) 129bool(false) 130bool(true) 131int(1024) 132string(32) "950b7457d1deb6332f2fc5d42f3129d6" 133 134-- Testing fwrite() with file having content of type text -- 135-- Opening file in r -- 136int(0) 137 138Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 139bool(false) 140int(0) 141bool(false) 142int(2) 143 144Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 145bool(false) 146int(2) 147bool(false) 148bool(true) 149int(1024) 150string(32) "e486000c4c8452774f746a27658d87fa" 151-- Opening file in rb -- 152int(0) 153 154Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 155bool(false) 156int(0) 157bool(false) 158int(2) 159 160Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 161bool(false) 162int(2) 163bool(false) 164bool(true) 165int(1024) 166string(32) "e486000c4c8452774f746a27658d87fa" 167-- Opening file in rt -- 168int(0) 169 170Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 171bool(false) 172int(0) 173bool(false) 174int(2) 175 176Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 177bool(false) 178int(2) 179bool(false) 180bool(true) 181int(1024) 182string(32) "e486000c4c8452774f746a27658d87fa" 183 184-- Testing fwrite() with file having content of type text_with_new_line -- 185-- Opening file in r -- 186int(0) 187 188Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 189bool(false) 190int(0) 191bool(false) 192int(2) 193 194Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 195bool(false) 196int(2) 197bool(false) 198bool(true) 199int(1024) 200string(32) "b09c8026a64a88d36d4c2f17983964bb" 201-- Opening file in rb -- 202int(0) 203 204Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 205bool(false) 206int(0) 207bool(false) 208int(2) 209 210Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 211bool(false) 212int(2) 213bool(false) 214bool(true) 215int(1024) 216string(32) "b09c8026a64a88d36d4c2f17983964bb" 217-- Opening file in rt -- 218int(0) 219 220Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 221bool(false) 222int(0) 223bool(false) 224int(2) 225 226Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 227bool(false) 228int(2) 229bool(false) 230bool(true) 231int(1024) 232string(32) "b09c8026a64a88d36d4c2f17983964bb" 233 234-- Testing fwrite() with file having content of type alphanumeric -- 235-- Opening file in r -- 236int(0) 237 238Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 239bool(false) 240int(0) 241bool(false) 242int(2) 243 244Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 245bool(false) 246int(2) 247bool(false) 248bool(true) 249int(1024) 250string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 251-- Opening file in rb -- 252int(0) 253 254Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 255bool(false) 256int(0) 257bool(false) 258int(2) 259 260Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 261bool(false) 262int(2) 263bool(false) 264bool(true) 265int(1024) 266string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 267-- Opening file in rt -- 268int(0) 269 270Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 271bool(false) 272int(0) 273bool(false) 274int(2) 275 276Notice: fwrite(): write of 1024 bytes failed with errno=9 Bad file descriptor in %s on line %d 277bool(false) 278int(2) 279bool(false) 280bool(true) 281int(1024) 282string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 283Done 284