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