Return to fread_variation3.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / file |
1.1 misho 1: --TEST-- 2: Test fread() function : usage variations - read beyond file size, read/write mode 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: string fread ( resource $handle [, int $length] ); 13: Description: reads up to length bytes from the file pointer referenced by handle. 14: Reading stops when up to length bytes have been read, EOF (end of file) is 15: reached, (for network streams) when a packet becomes available, or (after 16: opening userspace stream) when 8192 bytes have been read whichever comes first. 17: */ 18: 19: // include the file.inc for common functions for test 20: include ("file.inc"); 21: 22: /* Function : function check_read(resource $file_handle, int $read_size, int $expect_size) 23: Description : Read data from file of size $read_size and verifies that $expected_size no. of 24: bytes are read. 25: $file_handle : File Handle 26: $read_size : No. of bytes to be read. 27: $expect_size : Expected data length 28: Returns: returns the data read 29: */ 30: function check_read($file_handle, $read_size, $expect_size) { 31: // print file pointer position before read 32: var_dump( ftell($file_handle) ); 33: var_dump( feof($file_handle) ); 34: 35: // read the data of size $read_size 36: echo "Reading $read_size bytes from file, expecting $expect_size bytes ... "; 37: $data_from_file = fread($file_handle, $read_size); 38: 39: // check if data read is of expected size 40: if ( strlen($data_from_file) == $expect_size) 41: echo "OK\n"; 42: else 43: echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n"; 44: 45: // file pointer position after read 46: var_dump( ftell($file_handle) ); 47: // check if file pointer at eof() 48: var_dump( feof($file_handle) ); 49: 50: return $data_from_file; 51: } 52: 53: echo "*** Testing fread() : usage variations ***\n"; 54: 55: $file_modes = array("a+","a+b","a+t", 56: "w+","w+b","w+t", 57: "x+","x+b","x+t"); 58: 59: $file_content_types = array("numeric","text","text_with_new_line"); 60: 61: foreach($file_content_types as $file_content_type) { 62: echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n"; 63: 64: /* open the file using $files_modes and perform fread() on it */ 65: foreach($file_modes as $file_mode) { 66: if(!strstr($file_mode,"x")){ 67: /* create files with $file_content_type */ 68: create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fread_variation", 3); 69: } 70: 71: $filename = dirname(__FILE__)."/fread_variation3.tmp"; // this is name of the file created by create_files() 72: echo "-- File opened in mode ".$file_mode." --\n"; 73: $file_handle = fopen($filename, $file_mode); 74: if (!$file_handle) { 75: echo "Error: failed to fopen() file: $filename!"; 76: exit(); 77: } 78: 79: if(strstr($file_mode,"w") || strstr($file_mode,"x") ) { 80: $data_to_be_written=""; 81: fill_file($file_handle, $file_content_type, 1024); 82: } 83: 84: rewind($file_handle); 85: 86: // read file by giving size more than its size 87: echo "-- Reading beyond filesize, expeceted : 1024 bytes --\n"; 88: rewind($file_handle); 89: $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 1024) ); 90: if ( $data_from_file != false) 91: var_dump( md5($data_from_file) ); 92: 93: rewind($file_handle); 94: echo "-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --\n"; 95: // try fread when file pointer at end 96: fseek($file_handle, 0, SEEK_END); 97: //reading file when file pointer at end 98: $data_from_file = check_read($file_handle, 10, 0); 99: if ( $data_from_file != false) 100: var_dump( md5($data_from_file) ); 101: 102: // now close the file 103: fclose($file_handle); 104: 105: // delete the file created 106: delete_file($filename); // delete file 107: } // end of inner foreach loop 108: }// end of outer foreach loop 109: 110: echo"Done\n"; 111: ?> 112: --EXPECTF-- 113: *** Testing fread() : usage variations *** 114: 115: -- Testing fread() with file having content of type numeric -- 116: -- File opened in mode a+ -- 117: -- Reading beyond filesize, expeceted : 1024 bytes -- 118: int(0) 119: bool(false) 120: Reading 1030 bytes from file, expecting 1024 bytes ... OK 121: int(1024) 122: bool(true) 123: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 124: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 125: int(1024) 126: bool(false) 127: Reading 10 bytes from file, expecting 0 bytes ... OK 128: int(1024) 129: bool(true) 130: -- File opened in mode a+b -- 131: -- Reading beyond filesize, expeceted : 1024 bytes -- 132: int(0) 133: bool(false) 134: Reading 1030 bytes from file, expecting 1024 bytes ... OK 135: int(1024) 136: bool(true) 137: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 138: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 139: int(1024) 140: bool(false) 141: Reading 10 bytes from file, expecting 0 bytes ... OK 142: int(1024) 143: bool(true) 144: -- File opened in mode a+t -- 145: -- Reading beyond filesize, expeceted : 1024 bytes -- 146: int(0) 147: bool(false) 148: Reading 1030 bytes from file, expecting 1024 bytes ... OK 149: int(1024) 150: bool(true) 151: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 152: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 153: int(1024) 154: bool(false) 155: Reading 10 bytes from file, expecting 0 bytes ... OK 156: int(1024) 157: bool(true) 158: -- File opened in mode w+ -- 159: -- Reading beyond filesize, expeceted : 1024 bytes -- 160: int(0) 161: bool(false) 162: Reading 1030 bytes from file, expecting 1024 bytes ... OK 163: int(1024) 164: bool(true) 165: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 166: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 167: int(1024) 168: bool(false) 169: Reading 10 bytes from file, expecting 0 bytes ... OK 170: int(1024) 171: bool(true) 172: -- File opened in mode w+b -- 173: -- Reading beyond filesize, expeceted : 1024 bytes -- 174: int(0) 175: bool(false) 176: Reading 1030 bytes from file, expecting 1024 bytes ... OK 177: int(1024) 178: bool(true) 179: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 180: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 181: int(1024) 182: bool(false) 183: Reading 10 bytes from file, expecting 0 bytes ... OK 184: int(1024) 185: bool(true) 186: -- File opened in mode w+t -- 187: -- Reading beyond filesize, expeceted : 1024 bytes -- 188: int(0) 189: bool(false) 190: Reading 1030 bytes from file, expecting 1024 bytes ... OK 191: int(1024) 192: bool(true) 193: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 194: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 195: int(1024) 196: bool(false) 197: Reading 10 bytes from file, expecting 0 bytes ... OK 198: int(1024) 199: bool(true) 200: -- File opened in mode x+ -- 201: -- Reading beyond filesize, expeceted : 1024 bytes -- 202: int(0) 203: bool(false) 204: Reading 1030 bytes from file, expecting 1024 bytes ... OK 205: int(1024) 206: bool(true) 207: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 208: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 209: int(1024) 210: bool(false) 211: Reading 10 bytes from file, expecting 0 bytes ... OK 212: int(1024) 213: bool(true) 214: -- File opened in mode x+b -- 215: -- Reading beyond filesize, expeceted : 1024 bytes -- 216: int(0) 217: bool(false) 218: Reading 1030 bytes from file, expecting 1024 bytes ... OK 219: int(1024) 220: bool(true) 221: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 222: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 223: int(1024) 224: bool(false) 225: Reading 10 bytes from file, expecting 0 bytes ... OK 226: int(1024) 227: bool(true) 228: -- File opened in mode x+t -- 229: -- Reading beyond filesize, expeceted : 1024 bytes -- 230: int(0) 231: bool(false) 232: Reading 1030 bytes from file, expecting 1024 bytes ... OK 233: int(1024) 234: bool(true) 235: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 236: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 237: int(1024) 238: bool(false) 239: Reading 10 bytes from file, expecting 0 bytes ... OK 240: int(1024) 241: bool(true) 242: 243: -- Testing fread() with file having content of type text -- 244: -- File opened in mode a+ -- 245: -- Reading beyond filesize, expeceted : 1024 bytes -- 246: int(0) 247: bool(false) 248: Reading 1030 bytes from file, expecting 1024 bytes ... OK 249: int(1024) 250: bool(true) 251: string(32) "e486000c4c8452774f746a27658d87fa" 252: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 253: int(1024) 254: bool(false) 255: Reading 10 bytes from file, expecting 0 bytes ... OK 256: int(1024) 257: bool(true) 258: -- File opened in mode a+b -- 259: -- Reading beyond filesize, expeceted : 1024 bytes -- 260: int(0) 261: bool(false) 262: Reading 1030 bytes from file, expecting 1024 bytes ... OK 263: int(1024) 264: bool(true) 265: string(32) "e486000c4c8452774f746a27658d87fa" 266: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 267: int(1024) 268: bool(false) 269: Reading 10 bytes from file, expecting 0 bytes ... OK 270: int(1024) 271: bool(true) 272: -- File opened in mode a+t -- 273: -- Reading beyond filesize, expeceted : 1024 bytes -- 274: int(0) 275: bool(false) 276: Reading 1030 bytes from file, expecting 1024 bytes ... OK 277: int(1024) 278: bool(true) 279: string(32) "e486000c4c8452774f746a27658d87fa" 280: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 281: int(1024) 282: bool(false) 283: Reading 10 bytes from file, expecting 0 bytes ... OK 284: int(1024) 285: bool(true) 286: -- File opened in mode w+ -- 287: -- Reading beyond filesize, expeceted : 1024 bytes -- 288: int(0) 289: bool(false) 290: Reading 1030 bytes from file, expecting 1024 bytes ... OK 291: int(1024) 292: bool(true) 293: string(32) "e486000c4c8452774f746a27658d87fa" 294: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 295: int(1024) 296: bool(false) 297: Reading 10 bytes from file, expecting 0 bytes ... OK 298: int(1024) 299: bool(true) 300: -- File opened in mode w+b -- 301: -- Reading beyond filesize, expeceted : 1024 bytes -- 302: int(0) 303: bool(false) 304: Reading 1030 bytes from file, expecting 1024 bytes ... OK 305: int(1024) 306: bool(true) 307: string(32) "e486000c4c8452774f746a27658d87fa" 308: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 309: int(1024) 310: bool(false) 311: Reading 10 bytes from file, expecting 0 bytes ... OK 312: int(1024) 313: bool(true) 314: -- File opened in mode w+t -- 315: -- Reading beyond filesize, expeceted : 1024 bytes -- 316: int(0) 317: bool(false) 318: Reading 1030 bytes from file, expecting 1024 bytes ... OK 319: int(1024) 320: bool(true) 321: string(32) "e486000c4c8452774f746a27658d87fa" 322: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 323: int(1024) 324: bool(false) 325: Reading 10 bytes from file, expecting 0 bytes ... OK 326: int(1024) 327: bool(true) 328: -- File opened in mode x+ -- 329: -- Reading beyond filesize, expeceted : 1024 bytes -- 330: int(0) 331: bool(false) 332: Reading 1030 bytes from file, expecting 1024 bytes ... OK 333: int(1024) 334: bool(true) 335: string(32) "e486000c4c8452774f746a27658d87fa" 336: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 337: int(1024) 338: bool(false) 339: Reading 10 bytes from file, expecting 0 bytes ... OK 340: int(1024) 341: bool(true) 342: -- File opened in mode x+b -- 343: -- Reading beyond filesize, expeceted : 1024 bytes -- 344: int(0) 345: bool(false) 346: Reading 1030 bytes from file, expecting 1024 bytes ... OK 347: int(1024) 348: bool(true) 349: string(32) "e486000c4c8452774f746a27658d87fa" 350: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 351: int(1024) 352: bool(false) 353: Reading 10 bytes from file, expecting 0 bytes ... OK 354: int(1024) 355: bool(true) 356: -- File opened in mode x+t -- 357: -- Reading beyond filesize, expeceted : 1024 bytes -- 358: int(0) 359: bool(false) 360: Reading 1030 bytes from file, expecting 1024 bytes ... OK 361: int(1024) 362: bool(true) 363: string(32) "e486000c4c8452774f746a27658d87fa" 364: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 365: int(1024) 366: bool(false) 367: Reading 10 bytes from file, expecting 0 bytes ... OK 368: int(1024) 369: bool(true) 370: 371: -- Testing fread() with file having content of type text_with_new_line -- 372: -- File opened in mode a+ -- 373: -- Reading beyond filesize, expeceted : 1024 bytes -- 374: int(0) 375: bool(false) 376: Reading 1030 bytes from file, expecting 1024 bytes ... OK 377: int(1024) 378: bool(true) 379: string(32) "b09c8026a64a88d36d4c2f17983964bb" 380: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 381: int(1024) 382: bool(false) 383: Reading 10 bytes from file, expecting 0 bytes ... OK 384: int(1024) 385: bool(true) 386: -- File opened in mode a+b -- 387: -- Reading beyond filesize, expeceted : 1024 bytes -- 388: int(0) 389: bool(false) 390: Reading 1030 bytes from file, expecting 1024 bytes ... OK 391: int(1024) 392: bool(true) 393: string(32) "b09c8026a64a88d36d4c2f17983964bb" 394: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 395: int(1024) 396: bool(false) 397: Reading 10 bytes from file, expecting 0 bytes ... OK 398: int(1024) 399: bool(true) 400: -- File opened in mode a+t -- 401: -- Reading beyond filesize, expeceted : 1024 bytes -- 402: int(0) 403: bool(false) 404: Reading 1030 bytes from file, expecting 1024 bytes ... OK 405: int(1024) 406: bool(true) 407: string(32) "b09c8026a64a88d36d4c2f17983964bb" 408: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 409: int(1024) 410: bool(false) 411: Reading 10 bytes from file, expecting 0 bytes ... OK 412: int(1024) 413: bool(true) 414: -- File opened in mode w+ -- 415: -- Reading beyond filesize, expeceted : 1024 bytes -- 416: int(0) 417: bool(false) 418: Reading 1030 bytes from file, expecting 1024 bytes ... OK 419: int(1024) 420: bool(true) 421: string(32) "b09c8026a64a88d36d4c2f17983964bb" 422: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 423: int(1024) 424: bool(false) 425: Reading 10 bytes from file, expecting 0 bytes ... OK 426: int(1024) 427: bool(true) 428: -- File opened in mode w+b -- 429: -- Reading beyond filesize, expeceted : 1024 bytes -- 430: int(0) 431: bool(false) 432: Reading 1030 bytes from file, expecting 1024 bytes ... OK 433: int(1024) 434: bool(true) 435: string(32) "b09c8026a64a88d36d4c2f17983964bb" 436: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 437: int(1024) 438: bool(false) 439: Reading 10 bytes from file, expecting 0 bytes ... OK 440: int(1024) 441: bool(true) 442: -- File opened in mode w+t -- 443: -- Reading beyond filesize, expeceted : 1024 bytes -- 444: int(0) 445: bool(false) 446: Reading 1030 bytes from file, expecting 1024 bytes ... OK 447: int(1024) 448: bool(true) 449: string(32) "b09c8026a64a88d36d4c2f17983964bb" 450: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 451: int(1024) 452: bool(false) 453: Reading 10 bytes from file, expecting 0 bytes ... OK 454: int(1024) 455: bool(true) 456: -- File opened in mode x+ -- 457: -- Reading beyond filesize, expeceted : 1024 bytes -- 458: int(0) 459: bool(false) 460: Reading 1030 bytes from file, expecting 1024 bytes ... OK 461: int(1024) 462: bool(true) 463: string(32) "b09c8026a64a88d36d4c2f17983964bb" 464: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 465: int(1024) 466: bool(false) 467: Reading 10 bytes from file, expecting 0 bytes ... OK 468: int(1024) 469: bool(true) 470: -- File opened in mode x+b -- 471: -- Reading beyond filesize, expeceted : 1024 bytes -- 472: int(0) 473: bool(false) 474: Reading 1030 bytes from file, expecting 1024 bytes ... OK 475: int(1024) 476: bool(true) 477: string(32) "b09c8026a64a88d36d4c2f17983964bb" 478: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 479: int(1024) 480: bool(false) 481: Reading 10 bytes from file, expecting 0 bytes ... OK 482: int(1024) 483: bool(true) 484: -- File opened in mode x+t -- 485: -- Reading beyond filesize, expeceted : 1024 bytes -- 486: int(0) 487: bool(false) 488: Reading 1030 bytes from file, expecting 1024 bytes ... OK 489: int(1024) 490: bool(true) 491: string(32) "b09c8026a64a88d36d4c2f17983964bb" 492: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 493: int(1024) 494: bool(false) 495: Reading 10 bytes from file, expecting 0 bytes ... OK 496: int(1024) 497: bool(true) 498: Done