1--TEST-- 2Test fread() function : usage variations - read some/all chars, read/write modes 3--FILE-- 4<?php 5/* Read content less than file size & 6 Read entire file 7*/ 8 9// include the file.inc for common functions for test 10include ("file.inc"); 11 12/* Function : function check_read(resource $file_handle, int $read_size, int $expect_size) 13 Description : Read data from file of size $read_size and verifies that $expected_size no. of 14 bytes are read. 15 $file_handle : File Handle 16 $read_size : No. of bytes to be read. 17 $expect_size : Expected data length 18 Returns: returns the data read 19*/ 20function check_read($file_handle, $read_size, $expect_size) { 21 // print file pointer position before read 22 var_dump( ftell($file_handle) ); 23 var_dump( feof($file_handle) ); 24 25 // read the data of size $read_size 26 echo "Reading $read_size bytes from file, expecting $expect_size bytes ... "; 27 $data_from_file = fread($file_handle, $read_size); 28 29 // check if data read is of expected size 30 if ( strlen($data_from_file) == $expect_size) 31 echo "OK\n"; 32 else 33 echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n"; 34 35 // file pointer position after read 36 var_dump( ftell($file_handle) ); 37 // check if file pointer at eof() 38 var_dump( feof($file_handle) ); 39 40 return $data_from_file; 41} 42 43echo "*** Testing fread() : usage variations ***\n"; 44 45$file_modes = array("a+","a+b","a+t", 46 "w+","w+b","w+t", 47 "x+","x+b","x+t"); 48 49$file_content_types = array("numeric","text","text_with_new_line", "alphanumeric"); 50 51foreach($file_content_types as $file_content_type) { 52 echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n"; 53 54 /* open the file using $files_modes and perform fread() on it */ 55 foreach($file_modes as $file_mode) { 56 if(!strstr($file_mode,"x")){ 57 /* create files with $file_content_type */ 58 create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "fread_variation"); 59 } 60 61 $filename = __DIR__."/fread_variation1.tmp"; // this is name of the file created by create_files() 62 echo "-- File opened in mode ".$file_mode." --\n"; 63 $file_handle = fopen($filename, $file_mode); 64 if (!$file_handle) { 65 echo "Error: failed to fopen() file: $filename!"; 66 exit(); 67 } 68 69 if(strstr($file_mode,"w") || strstr($file_mode,"x") ) { 70 fill_file($file_handle, $file_content_type, 1024); 71 } 72 73 rewind($file_handle); 74 echo "-- Reading entire file content, expected : 1024 bytes --\n"; 75 // read from file, by giving the file actual size, 76 $data_from_file = check_read($file_handle, 1024, (strstr($file_mode, "+") ? 1024 : 1024 ) ); 77 // calculate the hash and dump it, if data read, expecting here no data was read 78 if ( $data_from_file != false) 79 var_dump( md5($data_from_file) ); 80 81 // reading file by giving less than its size 82 echo "-- Reading file content less than max. file size, expected : 1000 bytes --\n"; 83 rewind($file_handle); 84 $data_from_file = check_read($file_handle, 1000, (strstr($file_mode, "+") ? 1000 : 1000 ) ); 85 // calculate the hash and dump it, if data read, expecting here no data was read 86 if ( $data_from_file != false) 87 var_dump( md5($data_from_file) ); 88 89 // now close the file 90 fclose($file_handle); 91 92 // delete the file created 93 delete_file($filename); // delete file 94 } // end of inner foreach loop 95}// end of outer foreach loop 96 97echo"Done\n"; 98?> 99--EXPECT-- 100*** Testing fread() : usage variations *** 101 102-- Testing fread() with file having content of type numeric -- 103-- File opened in mode a+ -- 104-- Reading entire file content, expected : 1024 bytes -- 105int(0) 106bool(false) 107Reading 1024 bytes from file, expecting 1024 bytes ... OK 108int(1024) 109bool(false) 110string(32) "950b7457d1deb6332f2fc5d42f3129d6" 111-- Reading file content less than max. file size, expected : 1000 bytes -- 112int(0) 113bool(false) 114Reading 1000 bytes from file, expecting 1000 bytes ... OK 115int(1000) 116bool(false) 117string(32) "4501f99f2b79d0345f26f1394aca58a3" 118-- File opened in mode a+b -- 119-- Reading entire file content, expected : 1024 bytes -- 120int(0) 121bool(false) 122Reading 1024 bytes from file, expecting 1024 bytes ... OK 123int(1024) 124bool(false) 125string(32) "950b7457d1deb6332f2fc5d42f3129d6" 126-- Reading file content less than max. file size, expected : 1000 bytes -- 127int(0) 128bool(false) 129Reading 1000 bytes from file, expecting 1000 bytes ... OK 130int(1000) 131bool(false) 132string(32) "4501f99f2b79d0345f26f1394aca58a3" 133-- File opened in mode a+t -- 134-- Reading entire file content, expected : 1024 bytes -- 135int(0) 136bool(false) 137Reading 1024 bytes from file, expecting 1024 bytes ... OK 138int(1024) 139bool(false) 140string(32) "950b7457d1deb6332f2fc5d42f3129d6" 141-- Reading file content less than max. file size, expected : 1000 bytes -- 142int(0) 143bool(false) 144Reading 1000 bytes from file, expecting 1000 bytes ... OK 145int(1000) 146bool(false) 147string(32) "4501f99f2b79d0345f26f1394aca58a3" 148-- File opened in mode w+ -- 149-- Reading entire file content, expected : 1024 bytes -- 150int(0) 151bool(false) 152Reading 1024 bytes from file, expecting 1024 bytes ... OK 153int(1024) 154bool(false) 155string(32) "950b7457d1deb6332f2fc5d42f3129d6" 156-- Reading file content less than max. file size, expected : 1000 bytes -- 157int(0) 158bool(false) 159Reading 1000 bytes from file, expecting 1000 bytes ... OK 160int(1000) 161bool(false) 162string(32) "4501f99f2b79d0345f26f1394aca58a3" 163-- File opened in mode w+b -- 164-- Reading entire file content, expected : 1024 bytes -- 165int(0) 166bool(false) 167Reading 1024 bytes from file, expecting 1024 bytes ... OK 168int(1024) 169bool(false) 170string(32) "950b7457d1deb6332f2fc5d42f3129d6" 171-- Reading file content less than max. file size, expected : 1000 bytes -- 172int(0) 173bool(false) 174Reading 1000 bytes from file, expecting 1000 bytes ... OK 175int(1000) 176bool(false) 177string(32) "4501f99f2b79d0345f26f1394aca58a3" 178-- File opened in mode w+t -- 179-- Reading entire file content, expected : 1024 bytes -- 180int(0) 181bool(false) 182Reading 1024 bytes from file, expecting 1024 bytes ... OK 183int(1024) 184bool(false) 185string(32) "950b7457d1deb6332f2fc5d42f3129d6" 186-- Reading file content less than max. file size, expected : 1000 bytes -- 187int(0) 188bool(false) 189Reading 1000 bytes from file, expecting 1000 bytes ... OK 190int(1000) 191bool(false) 192string(32) "4501f99f2b79d0345f26f1394aca58a3" 193-- File opened in mode x+ -- 194-- Reading entire file content, expected : 1024 bytes -- 195int(0) 196bool(false) 197Reading 1024 bytes from file, expecting 1024 bytes ... OK 198int(1024) 199bool(false) 200string(32) "950b7457d1deb6332f2fc5d42f3129d6" 201-- Reading file content less than max. file size, expected : 1000 bytes -- 202int(0) 203bool(false) 204Reading 1000 bytes from file, expecting 1000 bytes ... OK 205int(1000) 206bool(false) 207string(32) "4501f99f2b79d0345f26f1394aca58a3" 208-- File opened in mode x+b -- 209-- Reading entire file content, expected : 1024 bytes -- 210int(0) 211bool(false) 212Reading 1024 bytes from file, expecting 1024 bytes ... OK 213int(1024) 214bool(false) 215string(32) "950b7457d1deb6332f2fc5d42f3129d6" 216-- Reading file content less than max. file size, expected : 1000 bytes -- 217int(0) 218bool(false) 219Reading 1000 bytes from file, expecting 1000 bytes ... OK 220int(1000) 221bool(false) 222string(32) "4501f99f2b79d0345f26f1394aca58a3" 223-- File opened in mode x+t -- 224-- Reading entire file content, expected : 1024 bytes -- 225int(0) 226bool(false) 227Reading 1024 bytes from file, expecting 1024 bytes ... OK 228int(1024) 229bool(false) 230string(32) "950b7457d1deb6332f2fc5d42f3129d6" 231-- Reading file content less than max. file size, expected : 1000 bytes -- 232int(0) 233bool(false) 234Reading 1000 bytes from file, expecting 1000 bytes ... OK 235int(1000) 236bool(false) 237string(32) "4501f99f2b79d0345f26f1394aca58a3" 238 239-- Testing fread() with file having content of type text -- 240-- File opened in mode a+ -- 241-- Reading entire file content, expected : 1024 bytes -- 242int(0) 243bool(false) 244Reading 1024 bytes from file, expecting 1024 bytes ... OK 245int(1024) 246bool(false) 247string(32) "e486000c4c8452774f746a27658d87fa" 248-- Reading file content less than max. file size, expected : 1000 bytes -- 249int(0) 250bool(false) 251Reading 1000 bytes from file, expecting 1000 bytes ... OK 252int(1000) 253bool(false) 254string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 255-- File opened in mode a+b -- 256-- Reading entire file content, expected : 1024 bytes -- 257int(0) 258bool(false) 259Reading 1024 bytes from file, expecting 1024 bytes ... OK 260int(1024) 261bool(false) 262string(32) "e486000c4c8452774f746a27658d87fa" 263-- Reading file content less than max. file size, expected : 1000 bytes -- 264int(0) 265bool(false) 266Reading 1000 bytes from file, expecting 1000 bytes ... OK 267int(1000) 268bool(false) 269string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 270-- File opened in mode a+t -- 271-- Reading entire file content, expected : 1024 bytes -- 272int(0) 273bool(false) 274Reading 1024 bytes from file, expecting 1024 bytes ... OK 275int(1024) 276bool(false) 277string(32) "e486000c4c8452774f746a27658d87fa" 278-- Reading file content less than max. file size, expected : 1000 bytes -- 279int(0) 280bool(false) 281Reading 1000 bytes from file, expecting 1000 bytes ... OK 282int(1000) 283bool(false) 284string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 285-- File opened in mode w+ -- 286-- Reading entire file content, expected : 1024 bytes -- 287int(0) 288bool(false) 289Reading 1024 bytes from file, expecting 1024 bytes ... OK 290int(1024) 291bool(false) 292string(32) "e486000c4c8452774f746a27658d87fa" 293-- Reading file content less than max. file size, expected : 1000 bytes -- 294int(0) 295bool(false) 296Reading 1000 bytes from file, expecting 1000 bytes ... OK 297int(1000) 298bool(false) 299string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 300-- File opened in mode w+b -- 301-- Reading entire file content, expected : 1024 bytes -- 302int(0) 303bool(false) 304Reading 1024 bytes from file, expecting 1024 bytes ... OK 305int(1024) 306bool(false) 307string(32) "e486000c4c8452774f746a27658d87fa" 308-- Reading file content less than max. file size, expected : 1000 bytes -- 309int(0) 310bool(false) 311Reading 1000 bytes from file, expecting 1000 bytes ... OK 312int(1000) 313bool(false) 314string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 315-- File opened in mode w+t -- 316-- Reading entire file content, expected : 1024 bytes -- 317int(0) 318bool(false) 319Reading 1024 bytes from file, expecting 1024 bytes ... OK 320int(1024) 321bool(false) 322string(32) "e486000c4c8452774f746a27658d87fa" 323-- Reading file content less than max. file size, expected : 1000 bytes -- 324int(0) 325bool(false) 326Reading 1000 bytes from file, expecting 1000 bytes ... OK 327int(1000) 328bool(false) 329string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 330-- File opened in mode x+ -- 331-- Reading entire file content, expected : 1024 bytes -- 332int(0) 333bool(false) 334Reading 1024 bytes from file, expecting 1024 bytes ... OK 335int(1024) 336bool(false) 337string(32) "e486000c4c8452774f746a27658d87fa" 338-- Reading file content less than max. file size, expected : 1000 bytes -- 339int(0) 340bool(false) 341Reading 1000 bytes from file, expecting 1000 bytes ... OK 342int(1000) 343bool(false) 344string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 345-- File opened in mode x+b -- 346-- Reading entire file content, expected : 1024 bytes -- 347int(0) 348bool(false) 349Reading 1024 bytes from file, expecting 1024 bytes ... OK 350int(1024) 351bool(false) 352string(32) "e486000c4c8452774f746a27658d87fa" 353-- Reading file content less than max. file size, expected : 1000 bytes -- 354int(0) 355bool(false) 356Reading 1000 bytes from file, expecting 1000 bytes ... OK 357int(1000) 358bool(false) 359string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 360-- File opened in mode x+t -- 361-- Reading entire file content, expected : 1024 bytes -- 362int(0) 363bool(false) 364Reading 1024 bytes from file, expecting 1024 bytes ... OK 365int(1024) 366bool(false) 367string(32) "e486000c4c8452774f746a27658d87fa" 368-- Reading file content less than max. file size, expected : 1000 bytes -- 369int(0) 370bool(false) 371Reading 1000 bytes from file, expecting 1000 bytes ... OK 372int(1000) 373bool(false) 374string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 375 376-- Testing fread() with file having content of type text_with_new_line -- 377-- File opened in mode a+ -- 378-- Reading entire file content, expected : 1024 bytes -- 379int(0) 380bool(false) 381Reading 1024 bytes from file, expecting 1024 bytes ... OK 382int(1024) 383bool(false) 384string(32) "b09c8026a64a88d36d4c2f17983964bb" 385-- Reading file content less than max. file size, expected : 1000 bytes -- 386int(0) 387bool(false) 388Reading 1000 bytes from file, expecting 1000 bytes ... OK 389int(1000) 390bool(false) 391string(32) "a148fa8110bbac875d84fc9d7056c0a1" 392-- File opened in mode a+b -- 393-- Reading entire file content, expected : 1024 bytes -- 394int(0) 395bool(false) 396Reading 1024 bytes from file, expecting 1024 bytes ... OK 397int(1024) 398bool(false) 399string(32) "b09c8026a64a88d36d4c2f17983964bb" 400-- Reading file content less than max. file size, expected : 1000 bytes -- 401int(0) 402bool(false) 403Reading 1000 bytes from file, expecting 1000 bytes ... OK 404int(1000) 405bool(false) 406string(32) "a148fa8110bbac875d84fc9d7056c0a1" 407-- File opened in mode a+t -- 408-- Reading entire file content, expected : 1024 bytes -- 409int(0) 410bool(false) 411Reading 1024 bytes from file, expecting 1024 bytes ... OK 412int(1024) 413bool(false) 414string(32) "b09c8026a64a88d36d4c2f17983964bb" 415-- Reading file content less than max. file size, expected : 1000 bytes -- 416int(0) 417bool(false) 418Reading 1000 bytes from file, expecting 1000 bytes ... OK 419int(1000) 420bool(false) 421string(32) "a148fa8110bbac875d84fc9d7056c0a1" 422-- File opened in mode w+ -- 423-- Reading entire file content, expected : 1024 bytes -- 424int(0) 425bool(false) 426Reading 1024 bytes from file, expecting 1024 bytes ... OK 427int(1024) 428bool(false) 429string(32) "b09c8026a64a88d36d4c2f17983964bb" 430-- Reading file content less than max. file size, expected : 1000 bytes -- 431int(0) 432bool(false) 433Reading 1000 bytes from file, expecting 1000 bytes ... OK 434int(1000) 435bool(false) 436string(32) "a148fa8110bbac875d84fc9d7056c0a1" 437-- File opened in mode w+b -- 438-- Reading entire file content, expected : 1024 bytes -- 439int(0) 440bool(false) 441Reading 1024 bytes from file, expecting 1024 bytes ... OK 442int(1024) 443bool(false) 444string(32) "b09c8026a64a88d36d4c2f17983964bb" 445-- Reading file content less than max. file size, expected : 1000 bytes -- 446int(0) 447bool(false) 448Reading 1000 bytes from file, expecting 1000 bytes ... OK 449int(1000) 450bool(false) 451string(32) "a148fa8110bbac875d84fc9d7056c0a1" 452-- File opened in mode w+t -- 453-- Reading entire file content, expected : 1024 bytes -- 454int(0) 455bool(false) 456Reading 1024 bytes from file, expecting 1024 bytes ... OK 457int(1024) 458bool(false) 459string(32) "b09c8026a64a88d36d4c2f17983964bb" 460-- Reading file content less than max. file size, expected : 1000 bytes -- 461int(0) 462bool(false) 463Reading 1000 bytes from file, expecting 1000 bytes ... OK 464int(1000) 465bool(false) 466string(32) "a148fa8110bbac875d84fc9d7056c0a1" 467-- File opened in mode x+ -- 468-- Reading entire file content, expected : 1024 bytes -- 469int(0) 470bool(false) 471Reading 1024 bytes from file, expecting 1024 bytes ... OK 472int(1024) 473bool(false) 474string(32) "b09c8026a64a88d36d4c2f17983964bb" 475-- Reading file content less than max. file size, expected : 1000 bytes -- 476int(0) 477bool(false) 478Reading 1000 bytes from file, expecting 1000 bytes ... OK 479int(1000) 480bool(false) 481string(32) "a148fa8110bbac875d84fc9d7056c0a1" 482-- File opened in mode x+b -- 483-- Reading entire file content, expected : 1024 bytes -- 484int(0) 485bool(false) 486Reading 1024 bytes from file, expecting 1024 bytes ... OK 487int(1024) 488bool(false) 489string(32) "b09c8026a64a88d36d4c2f17983964bb" 490-- Reading file content less than max. file size, expected : 1000 bytes -- 491int(0) 492bool(false) 493Reading 1000 bytes from file, expecting 1000 bytes ... OK 494int(1000) 495bool(false) 496string(32) "a148fa8110bbac875d84fc9d7056c0a1" 497-- File opened in mode x+t -- 498-- Reading entire file content, expected : 1024 bytes -- 499int(0) 500bool(false) 501Reading 1024 bytes from file, expecting 1024 bytes ... OK 502int(1024) 503bool(false) 504string(32) "b09c8026a64a88d36d4c2f17983964bb" 505-- Reading file content less than max. file size, expected : 1000 bytes -- 506int(0) 507bool(false) 508Reading 1000 bytes from file, expecting 1000 bytes ... OK 509int(1000) 510bool(false) 511string(32) "a148fa8110bbac875d84fc9d7056c0a1" 512 513-- Testing fread() with file having content of type alphanumeric -- 514-- File opened in mode a+ -- 515-- Reading entire file content, expected : 1024 bytes -- 516int(0) 517bool(false) 518Reading 1024 bytes from file, expecting 1024 bytes ... OK 519int(1024) 520bool(false) 521string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 522-- Reading file content less than max. file size, expected : 1000 bytes -- 523int(0) 524bool(false) 525Reading 1000 bytes from file, expecting 1000 bytes ... OK 526int(1000) 527bool(false) 528string(32) "a49d752f980184c7f44568e930f89c72" 529-- File opened in mode a+b -- 530-- Reading entire file content, expected : 1024 bytes -- 531int(0) 532bool(false) 533Reading 1024 bytes from file, expecting 1024 bytes ... OK 534int(1024) 535bool(false) 536string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 537-- Reading file content less than max. file size, expected : 1000 bytes -- 538int(0) 539bool(false) 540Reading 1000 bytes from file, expecting 1000 bytes ... OK 541int(1000) 542bool(false) 543string(32) "a49d752f980184c7f44568e930f89c72" 544-- File opened in mode a+t -- 545-- Reading entire file content, expected : 1024 bytes -- 546int(0) 547bool(false) 548Reading 1024 bytes from file, expecting 1024 bytes ... OK 549int(1024) 550bool(false) 551string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 552-- Reading file content less than max. file size, expected : 1000 bytes -- 553int(0) 554bool(false) 555Reading 1000 bytes from file, expecting 1000 bytes ... OK 556int(1000) 557bool(false) 558string(32) "a49d752f980184c7f44568e930f89c72" 559-- File opened in mode w+ -- 560-- Reading entire file content, expected : 1024 bytes -- 561int(0) 562bool(false) 563Reading 1024 bytes from file, expecting 1024 bytes ... OK 564int(1024) 565bool(false) 566string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 567-- Reading file content less than max. file size, expected : 1000 bytes -- 568int(0) 569bool(false) 570Reading 1000 bytes from file, expecting 1000 bytes ... OK 571int(1000) 572bool(false) 573string(32) "a49d752f980184c7f44568e930f89c72" 574-- File opened in mode w+b -- 575-- Reading entire file content, expected : 1024 bytes -- 576int(0) 577bool(false) 578Reading 1024 bytes from file, expecting 1024 bytes ... OK 579int(1024) 580bool(false) 581string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 582-- Reading file content less than max. file size, expected : 1000 bytes -- 583int(0) 584bool(false) 585Reading 1000 bytes from file, expecting 1000 bytes ... OK 586int(1000) 587bool(false) 588string(32) "a49d752f980184c7f44568e930f89c72" 589-- File opened in mode w+t -- 590-- Reading entire file content, expected : 1024 bytes -- 591int(0) 592bool(false) 593Reading 1024 bytes from file, expecting 1024 bytes ... OK 594int(1024) 595bool(false) 596string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 597-- Reading file content less than max. file size, expected : 1000 bytes -- 598int(0) 599bool(false) 600Reading 1000 bytes from file, expecting 1000 bytes ... OK 601int(1000) 602bool(false) 603string(32) "a49d752f980184c7f44568e930f89c72" 604-- File opened in mode x+ -- 605-- Reading entire file content, expected : 1024 bytes -- 606int(0) 607bool(false) 608Reading 1024 bytes from file, expecting 1024 bytes ... OK 609int(1024) 610bool(false) 611string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 612-- Reading file content less than max. file size, expected : 1000 bytes -- 613int(0) 614bool(false) 615Reading 1000 bytes from file, expecting 1000 bytes ... OK 616int(1000) 617bool(false) 618string(32) "a49d752f980184c7f44568e930f89c72" 619-- File opened in mode x+b -- 620-- Reading entire file content, expected : 1024 bytes -- 621int(0) 622bool(false) 623Reading 1024 bytes from file, expecting 1024 bytes ... OK 624int(1024) 625bool(false) 626string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 627-- Reading file content less than max. file size, expected : 1000 bytes -- 628int(0) 629bool(false) 630Reading 1000 bytes from file, expecting 1000 bytes ... OK 631int(1000) 632bool(false) 633string(32) "a49d752f980184c7f44568e930f89c72" 634-- File opened in mode x+t -- 635-- Reading entire file content, expected : 1024 bytes -- 636int(0) 637bool(false) 638Reading 1024 bytes from file, expecting 1024 bytes ... OK 639int(1024) 640bool(false) 641string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 642-- Reading file content less than max. file size, expected : 1000 bytes -- 643int(0) 644bool(false) 645Reading 1000 bytes from file, expecting 1000 bytes ... OK 646int(1000) 647bool(false) 648string(32) "a49d752f980184c7f44568e930f89c72" 649Done 650