Return to fread_variation4.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, write only 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","ab","at", 56: "w","wb","wt", 57: "x","xb","xt" ); 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", 4); 69: } 70: 71: $filename = dirname(__FILE__)."/fread_variation4.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: fill_file($file_handle, $file_content_type, 1024); 81: } 82: 83: rewind($file_handle); 84: echo "-- Reading beyond filesize, expeceted : 1024 bytes --\n"; 85: // read file by giving size more than its size 86: rewind($file_handle); 87: $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 0) ); 88: if ( $data_from_file != false) 89: var_dump( md5($data_from_file) ); 90: 91: echo "-- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes --\n"; 92: rewind($file_handle); 93: // try fread when file pointer at end 94: fseek($file_handle, 0, SEEK_END); 95: //reading file when file pointer at end 96: $data_from_file = check_read($file_handle, 10, 0); 97: if ( $data_from_file != false) 98: var_dump( md5($data_from_file) ); 99: 100: // now close the file 101: fclose($file_handle); 102: 103: // delete the file created 104: delete_file($filename); // delete file 105: } // end of inner foreach loop 106: }// end of outer foreach loop 107: 108: echo"Done\n"; 109: ?> 110: --EXPECTF-- 111: *** Testing fread() : usage variations *** 112: 113: -- Testing fread() with file having content of type numeric -- 114: -- File opened in mode a -- 115: -- Reading beyond filesize, expeceted : 1024 bytes -- 116: int(0) 117: bool(false) 118: Reading 1030 bytes from file, expecting 0 bytes ... OK 119: int(0) 120: bool(false) 121: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 122: int(1024) 123: bool(false) 124: Reading 10 bytes from file, expecting 0 bytes ... OK 125: int(1024) 126: bool(false) 127: -- File opened in mode ab -- 128: -- Reading beyond filesize, expeceted : 1024 bytes -- 129: int(0) 130: bool(false) 131: Reading 1030 bytes from file, expecting 0 bytes ... OK 132: int(0) 133: bool(false) 134: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 135: int(1024) 136: bool(false) 137: Reading 10 bytes from file, expecting 0 bytes ... OK 138: int(1024) 139: bool(false) 140: -- File opened in mode at -- 141: -- Reading beyond filesize, expeceted : 1024 bytes -- 142: int(0) 143: bool(false) 144: Reading 1030 bytes from file, expecting 0 bytes ... OK 145: int(0) 146: bool(false) 147: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 148: int(1024) 149: bool(false) 150: Reading 10 bytes from file, expecting 0 bytes ... OK 151: int(1024) 152: bool(false) 153: -- File opened in mode w -- 154: -- Reading beyond filesize, expeceted : 1024 bytes -- 155: int(0) 156: bool(false) 157: Reading 1030 bytes from file, expecting 0 bytes ... OK 158: int(0) 159: bool(false) 160: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 161: int(1024) 162: bool(false) 163: Reading 10 bytes from file, expecting 0 bytes ... OK 164: int(1024) 165: bool(false) 166: -- File opened in mode wb -- 167: -- Reading beyond filesize, expeceted : 1024 bytes -- 168: int(0) 169: bool(false) 170: Reading 1030 bytes from file, expecting 0 bytes ... OK 171: int(0) 172: bool(false) 173: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 174: int(1024) 175: bool(false) 176: Reading 10 bytes from file, expecting 0 bytes ... OK 177: int(1024) 178: bool(false) 179: -- File opened in mode wt -- 180: -- Reading beyond filesize, expeceted : 1024 bytes -- 181: int(0) 182: bool(false) 183: Reading 1030 bytes from file, expecting 0 bytes ... OK 184: int(0) 185: bool(false) 186: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 187: int(1024) 188: bool(false) 189: Reading 10 bytes from file, expecting 0 bytes ... OK 190: int(1024) 191: bool(false) 192: -- File opened in mode x -- 193: -- Reading beyond filesize, expeceted : 1024 bytes -- 194: int(0) 195: bool(false) 196: Reading 1030 bytes from file, expecting 0 bytes ... OK 197: int(0) 198: bool(false) 199: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 200: int(1024) 201: bool(false) 202: Reading 10 bytes from file, expecting 0 bytes ... OK 203: int(1024) 204: bool(false) 205: -- File opened in mode xb -- 206: -- Reading beyond filesize, expeceted : 1024 bytes -- 207: int(0) 208: bool(false) 209: Reading 1030 bytes from file, expecting 0 bytes ... OK 210: int(0) 211: bool(false) 212: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 213: int(1024) 214: bool(false) 215: Reading 10 bytes from file, expecting 0 bytes ... OK 216: int(1024) 217: bool(false) 218: -- File opened in mode xt -- 219: -- Reading beyond filesize, expeceted : 1024 bytes -- 220: int(0) 221: bool(false) 222: Reading 1030 bytes from file, expecting 0 bytes ... OK 223: int(0) 224: bool(false) 225: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 226: int(1024) 227: bool(false) 228: Reading 10 bytes from file, expecting 0 bytes ... OK 229: int(1024) 230: bool(false) 231: 232: -- Testing fread() with file having content of type text -- 233: -- File opened in mode a -- 234: -- Reading beyond filesize, expeceted : 1024 bytes -- 235: int(0) 236: bool(false) 237: Reading 1030 bytes from file, expecting 0 bytes ... OK 238: int(0) 239: bool(false) 240: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 241: int(1024) 242: bool(false) 243: Reading 10 bytes from file, expecting 0 bytes ... OK 244: int(1024) 245: bool(false) 246: -- File opened in mode ab -- 247: -- Reading beyond filesize, expeceted : 1024 bytes -- 248: int(0) 249: bool(false) 250: Reading 1030 bytes from file, expecting 0 bytes ... OK 251: int(0) 252: bool(false) 253: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 254: int(1024) 255: bool(false) 256: Reading 10 bytes from file, expecting 0 bytes ... OK 257: int(1024) 258: bool(false) 259: -- File opened in mode at -- 260: -- Reading beyond filesize, expeceted : 1024 bytes -- 261: int(0) 262: bool(false) 263: Reading 1030 bytes from file, expecting 0 bytes ... OK 264: int(0) 265: bool(false) 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(false) 272: -- File opened in mode w -- 273: -- Reading beyond filesize, expeceted : 1024 bytes -- 274: int(0) 275: bool(false) 276: Reading 1030 bytes from file, expecting 0 bytes ... OK 277: int(0) 278: bool(false) 279: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 280: int(1024) 281: bool(false) 282: Reading 10 bytes from file, expecting 0 bytes ... OK 283: int(1024) 284: bool(false) 285: -- File opened in mode wb -- 286: -- Reading beyond filesize, expeceted : 1024 bytes -- 287: int(0) 288: bool(false) 289: Reading 1030 bytes from file, expecting 0 bytes ... OK 290: int(0) 291: bool(false) 292: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 293: int(1024) 294: bool(false) 295: Reading 10 bytes from file, expecting 0 bytes ... OK 296: int(1024) 297: bool(false) 298: -- File opened in mode wt -- 299: -- Reading beyond filesize, expeceted : 1024 bytes -- 300: int(0) 301: bool(false) 302: Reading 1030 bytes from file, expecting 0 bytes ... OK 303: int(0) 304: bool(false) 305: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 306: int(1024) 307: bool(false) 308: Reading 10 bytes from file, expecting 0 bytes ... OK 309: int(1024) 310: bool(false) 311: -- File opened in mode x -- 312: -- Reading beyond filesize, expeceted : 1024 bytes -- 313: int(0) 314: bool(false) 315: Reading 1030 bytes from file, expecting 0 bytes ... OK 316: int(0) 317: bool(false) 318: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 319: int(1024) 320: bool(false) 321: Reading 10 bytes from file, expecting 0 bytes ... OK 322: int(1024) 323: bool(false) 324: -- File opened in mode xb -- 325: -- Reading beyond filesize, expeceted : 1024 bytes -- 326: int(0) 327: bool(false) 328: Reading 1030 bytes from file, expecting 0 bytes ... OK 329: int(0) 330: bool(false) 331: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 332: int(1024) 333: bool(false) 334: Reading 10 bytes from file, expecting 0 bytes ... OK 335: int(1024) 336: bool(false) 337: -- File opened in mode xt -- 338: -- Reading beyond filesize, expeceted : 1024 bytes -- 339: int(0) 340: bool(false) 341: Reading 1030 bytes from file, expecting 0 bytes ... OK 342: int(0) 343: bool(false) 344: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 345: int(1024) 346: bool(false) 347: Reading 10 bytes from file, expecting 0 bytes ... OK 348: int(1024) 349: bool(false) 350: 351: -- Testing fread() with file having content of type text_with_new_line -- 352: -- File opened in mode a -- 353: -- Reading beyond filesize, expeceted : 1024 bytes -- 354: int(0) 355: bool(false) 356: Reading 1030 bytes from file, expecting 0 bytes ... OK 357: int(0) 358: bool(false) 359: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 360: int(1024) 361: bool(false) 362: Reading 10 bytes from file, expecting 0 bytes ... OK 363: int(1024) 364: bool(false) 365: -- File opened in mode ab -- 366: -- Reading beyond filesize, expeceted : 1024 bytes -- 367: int(0) 368: bool(false) 369: Reading 1030 bytes from file, expecting 0 bytes ... OK 370: int(0) 371: bool(false) 372: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 373: int(1024) 374: bool(false) 375: Reading 10 bytes from file, expecting 0 bytes ... OK 376: int(1024) 377: bool(false) 378: -- File opened in mode at -- 379: -- Reading beyond filesize, expeceted : 1024 bytes -- 380: int(0) 381: bool(false) 382: Reading 1030 bytes from file, expecting 0 bytes ... OK 383: int(0) 384: bool(false) 385: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 386: int(1024) 387: bool(false) 388: Reading 10 bytes from file, expecting 0 bytes ... OK 389: int(1024) 390: bool(false) 391: -- File opened in mode w -- 392: -- Reading beyond filesize, expeceted : 1024 bytes -- 393: int(0) 394: bool(false) 395: Reading 1030 bytes from file, expecting 0 bytes ... OK 396: int(0) 397: bool(false) 398: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 399: int(1024) 400: bool(false) 401: Reading 10 bytes from file, expecting 0 bytes ... OK 402: int(1024) 403: bool(false) 404: -- File opened in mode wb -- 405: -- Reading beyond filesize, expeceted : 1024 bytes -- 406: int(0) 407: bool(false) 408: Reading 1030 bytes from file, expecting 0 bytes ... OK 409: int(0) 410: bool(false) 411: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 412: int(1024) 413: bool(false) 414: Reading 10 bytes from file, expecting 0 bytes ... OK 415: int(1024) 416: bool(false) 417: -- File opened in mode wt -- 418: -- Reading beyond filesize, expeceted : 1024 bytes -- 419: int(0) 420: bool(false) 421: Reading 1030 bytes from file, expecting 0 bytes ... OK 422: int(0) 423: bool(false) 424: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 425: int(1024) 426: bool(false) 427: Reading 10 bytes from file, expecting 0 bytes ... OK 428: int(1024) 429: bool(false) 430: -- File opened in mode x -- 431: -- Reading beyond filesize, expeceted : 1024 bytes -- 432: int(0) 433: bool(false) 434: Reading 1030 bytes from file, expecting 0 bytes ... OK 435: int(0) 436: bool(false) 437: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 438: int(1024) 439: bool(false) 440: Reading 10 bytes from file, expecting 0 bytes ... OK 441: int(1024) 442: bool(false) 443: -- File opened in mode xb -- 444: -- Reading beyond filesize, expeceted : 1024 bytes -- 445: int(0) 446: bool(false) 447: Reading 1030 bytes from file, expecting 0 bytes ... OK 448: int(0) 449: bool(false) 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(false) 456: -- File opened in mode xt -- 457: -- Reading beyond filesize, expeceted : 1024 bytes -- 458: int(0) 459: bool(false) 460: Reading 1030 bytes from file, expecting 0 bytes ... OK 461: int(0) 462: bool(false) 463: -- Reading beyond filesize when file pointer pointing to EOF, expeceted : 0 bytes -- 464: int(1024) 465: bool(false) 466: Reading 10 bytes from file, expecting 0 bytes ... OK 467: int(1024) 468: bool(false) 469: Done