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