Return to fwrite_variation3.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / file |
1.1 misho 1: --TEST-- 2: Test fwrite() function : usage variations - a, ab, at, a+, a+b & a+t modes 3: --SKIPIF-- 4: <?php 5: if( substr(PHP_OS, 0, 3) == 'WIN' ) { 6: die('skip...Not valid for Windows'); 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: 20: echo "*** Testing fwrite() various operations ***\n"; 21: 22: // include the file.inc for Function: function delete_file($filename) 23: include ("file.inc"); 24: 25: /* 26: Test fwrite with file opened in mode : a,ab,at,a+,a+b,a+t 27: File having content of type numeric, text,text_with_new_line & alphanumeric 28: */ 29: 30: $file_modes = array("a","ab","at","a+","a+b","a+t"); 31: $file_content_types = array("numeric","text","text_with_new_line","alphanumeric"); 32: 33: 34: foreach($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 temp file and fill it content of type $file_content_type 42: $filename = dirname(__FILE__)."/fwrite_variation3.tmp"; // this is name of the file 43: create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fwrite_variation", 3); 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: // append the data to the file, starting from current position of the file pointer 58: var_dump( ftell($file_handle) ); // expected: 1024 59: var_dump( fwrite($file_handle,$data_to_be_written,400) ); 60: var_dump( ftell($file_handle) ); // expected: 1024 + 400 61: var_dump( feof($file_handle) ); // expected : true 62: 63: /*overwrite data in middle of the file*/ 64: fseek($file_handle, SEEK_SET, (1024 + 400)/2 ); 65: var_dump( ftell($file_handle)); // expected: (1024 + 400)/2 66: var_dump( fwrite($file_handle, $data_to_be_written, 200) ); 67: var_dump( ftell($file_handle) ); 68: var_dump( feof($file_handle) ); //Expecting bool(false) 69: 70: /* check the filesize and display file content */ 71: // close the file, get the size and content of the file. 72: var_dump( fclose($file_handle) ); 73: clearstatcache();//clears file status cache 74: var_dump( filesize($filename) ); 75: var_dump(md5(file_get_contents($filename))); 76: // delete the file created 77: delete_file($filename); // delete file with name fwrite_variation3.tmp 78: } // end of inner foreach loop 79: } // end of outer foreach loop 80: 81: echo "Done\n"; 82: ?> 83: --EXPECTF-- 84: *** Testing fwrite() various operations *** 85: 86: -- Testing fwrite() with file having content of type numeric -- 87: -- Opening file in a -- 88: int(0) 89: int(400) 90: int(400) 91: bool(false) 92: int(400) 93: int(200) 94: int(600) 95: bool(false) 96: bool(true) 97: int(1624) 98: string(32) "59ce5bf03b69069d00d6354bdc969ff6" 99: -- Opening file in ab -- 100: int(0) 101: int(400) 102: int(400) 103: bool(false) 104: int(400) 105: int(200) 106: int(600) 107: bool(false) 108: bool(true) 109: int(1624) 110: string(32) "59ce5bf03b69069d00d6354bdc969ff6" 111: -- Opening file in at -- 112: int(0) 113: int(400) 114: int(400) 115: bool(false) 116: int(400) 117: int(200) 118: int(600) 119: bool(false) 120: bool(true) 121: int(1624) 122: string(32) "59ce5bf03b69069d00d6354bdc969ff6" 123: -- Opening file in a+ -- 124: int(0) 125: int(400) 126: int(400) 127: bool(false) 128: int(400) 129: int(200) 130: int(600) 131: bool(false) 132: bool(true) 133: int(1624) 134: string(32) "59ce5bf03b69069d00d6354bdc969ff6" 135: -- Opening file in a+b -- 136: int(0) 137: int(400) 138: int(400) 139: bool(false) 140: int(400) 141: int(200) 142: int(600) 143: bool(false) 144: bool(true) 145: int(1624) 146: string(32) "59ce5bf03b69069d00d6354bdc969ff6" 147: -- Opening file in a+t -- 148: int(0) 149: int(400) 150: int(400) 151: bool(false) 152: int(400) 153: int(200) 154: int(600) 155: bool(false) 156: bool(true) 157: int(1624) 158: string(32) "59ce5bf03b69069d00d6354bdc969ff6" 159: 160: -- Testing fwrite() with file having content of type text -- 161: -- Opening file in a -- 162: int(0) 163: int(400) 164: int(400) 165: bool(false) 166: int(400) 167: int(200) 168: int(600) 169: bool(false) 170: bool(true) 171: int(1624) 172: string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 173: -- Opening file in ab -- 174: int(0) 175: int(400) 176: int(400) 177: bool(false) 178: int(400) 179: int(200) 180: int(600) 181: bool(false) 182: bool(true) 183: int(1624) 184: string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 185: -- Opening file in at -- 186: int(0) 187: int(400) 188: int(400) 189: bool(false) 190: int(400) 191: int(200) 192: int(600) 193: bool(false) 194: bool(true) 195: int(1624) 196: string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 197: -- Opening file in a+ -- 198: int(0) 199: int(400) 200: int(400) 201: bool(false) 202: int(400) 203: int(200) 204: int(600) 205: bool(false) 206: bool(true) 207: int(1624) 208: string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 209: -- Opening file in a+b -- 210: int(0) 211: int(400) 212: int(400) 213: bool(false) 214: int(400) 215: int(200) 216: int(600) 217: bool(false) 218: bool(true) 219: int(1624) 220: string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 221: -- Opening file in a+t -- 222: int(0) 223: int(400) 224: int(400) 225: bool(false) 226: int(400) 227: int(200) 228: int(600) 229: bool(false) 230: bool(true) 231: int(1624) 232: string(32) "dbd9dffd809d82e299bc1e5c55087f3b" 233: 234: -- Testing fwrite() with file having content of type text_with_new_line -- 235: -- Opening file in a -- 236: int(0) 237: int(400) 238: int(400) 239: bool(false) 240: int(400) 241: int(200) 242: int(600) 243: bool(false) 244: bool(true) 245: int(1624) 246: string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 247: -- Opening file in ab -- 248: int(0) 249: int(400) 250: int(400) 251: bool(false) 252: int(400) 253: int(200) 254: int(600) 255: bool(false) 256: bool(true) 257: int(1624) 258: string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 259: -- Opening file in at -- 260: int(0) 261: int(400) 262: int(400) 263: bool(false) 264: int(400) 265: int(200) 266: int(600) 267: bool(false) 268: bool(true) 269: int(1624) 270: string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 271: -- Opening file in a+ -- 272: int(0) 273: int(400) 274: int(400) 275: bool(false) 276: int(400) 277: int(200) 278: int(600) 279: bool(false) 280: bool(true) 281: int(1624) 282: string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 283: -- Opening file in a+b -- 284: int(0) 285: int(400) 286: int(400) 287: bool(false) 288: int(400) 289: int(200) 290: int(600) 291: bool(false) 292: bool(true) 293: int(1624) 294: string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 295: -- Opening file in a+t -- 296: int(0) 297: int(400) 298: int(400) 299: bool(false) 300: int(400) 301: int(200) 302: int(600) 303: bool(false) 304: bool(true) 305: int(1624) 306: string(32) "3f0a483fe8a2f405677844e0b1af6cf4" 307: 308: -- Testing fwrite() with file having content of type alphanumeric -- 309: -- Opening file in a -- 310: int(0) 311: int(400) 312: int(400) 313: bool(false) 314: int(400) 315: int(200) 316: int(600) 317: bool(false) 318: bool(true) 319: int(1624) 320: string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 321: -- Opening file in ab -- 322: int(0) 323: int(400) 324: int(400) 325: bool(false) 326: int(400) 327: int(200) 328: int(600) 329: bool(false) 330: bool(true) 331: int(1624) 332: string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 333: -- Opening file in at -- 334: int(0) 335: int(400) 336: int(400) 337: bool(false) 338: int(400) 339: int(200) 340: int(600) 341: bool(false) 342: bool(true) 343: int(1624) 344: string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 345: -- Opening file in a+ -- 346: int(0) 347: int(400) 348: int(400) 349: bool(false) 350: int(400) 351: int(200) 352: int(600) 353: bool(false) 354: bool(true) 355: int(1624) 356: string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 357: -- Opening file in a+b -- 358: int(0) 359: int(400) 360: int(400) 361: bool(false) 362: int(400) 363: int(200) 364: int(600) 365: bool(false) 366: bool(true) 367: int(1624) 368: string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 369: -- Opening file in a+t -- 370: int(0) 371: int(400) 372: int(400) 373: bool(false) 374: int(400) 375: int(200) 376: int(600) 377: bool(false) 378: bool(true) 379: int(1624) 380: string(32) "ea0c0bfa0b10aa8e614fd33ffe295cb9" 381: Done