123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- --TEST--
- Test fread() function : usage variations - read beyond file size, read/write mode
- --SKIPIF--
- <?php
- if (substr(PHP_OS, 0, 3) != 'WIN') {
- die('skip...only valid for Windows');
- }
- ?>
- --FILE--
- <?php
- // include the file.inc for common functions for test
- include ("file.inc");
- /* Function : function check_read(resource $file_handle, int $read_size, int $expect_size)
- Description : Read data from file of size $read_size and verifies that $expected_size no. of
- bytes are read.
- $file_handle : File Handle
- $read_size : No. of bytes to be read.
- $expect_size : Expected data length
- Returns: returns the data read
- */
- function check_read($file_handle, $read_size, $expect_size) {
- // print file pointer position before read
- var_dump( ftell($file_handle) );
- var_dump( feof($file_handle) );
- // read the data of size $read_size
- echo "Reading $read_size bytes from file, expecting $expect_size bytes ... ";
- $data_from_file = fread($file_handle, $read_size);
- // check if data read is of expected size
- if ( strlen($data_from_file) == $expect_size)
- echo "OK\n";
- else
- echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n";
- // file pointer position after read
- var_dump( ftell($file_handle) );
- // check if file pointer at eof()
- var_dump( feof($file_handle) );
- return $data_from_file;
- }
- echo "*** Testing fread() : usage variations ***\n";
- $file_modes = array("a+","a+b","a+t",
- "w+","w+b","w+t",
- "x+","x+b","x+t");
- $file_content_types = array("numeric","text","text_with_new_line");
- foreach($file_content_types as $file_content_type) {
- echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n";
- /* open the file using $files_modes and perform fread() on it */
- foreach($file_modes as $file_mode) {
- if(!strstr($file_mode,"x")){
- /* create files with $file_content_type */
- create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "私はガラスを食べられますfread_variation", 3);
- }
- $filename = __DIR__."/私はガラスを食べられますfread_variation3.tmp"; // this is name of the file created by create_files()
- echo "-- File opened in mode ".$file_mode." --\n";
- $file_handle = fopen($filename, $file_mode);
- if (!$file_handle) {
- echo "Error: failed to fopen() file: $filename!";
- exit();
- }
- if(strstr($file_mode,"w") || strstr($file_mode,"x") ) {
- $data_to_be_written="";
- fill_file($file_handle, $file_content_type, 1024);
- }
- rewind($file_handle);
- // read file by giving size more than its size
- echo "-- Reading beyond filesize, expected : 1024 bytes --\n";
- rewind($file_handle);
- $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 1024) );
- if ( $data_from_file != false)
- var_dump( md5($data_from_file) );
- rewind($file_handle);
- echo "-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --\n";
- // try fread when file pointer at end
- fseek($file_handle, 0, SEEK_END);
- //reading file when file pointer at end
- $data_from_file = check_read($file_handle, 10, 0);
- if ( $data_from_file != false)
- var_dump( md5($data_from_file) );
- // now close the file
- fclose($file_handle);
- // delete the file created
- delete_file($filename); // delete file
- } // end of inner foreach loop
- }// end of outer foreach loop
- echo"Done\n";
- ?>
- --EXPECT--
- *** Testing fread() : usage variations ***
- -- Testing fread() with file having content of type numeric --
- -- File opened in mode a+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode a+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode a+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "950b7457d1deb6332f2fc5d42f3129d6"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- Testing fread() with file having content of type text --
- -- File opened in mode a+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode a+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode a+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "e486000c4c8452774f746a27658d87fa"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- Testing fread() with file having content of type text_with_new_line --
- -- File opened in mode a+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode a+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode a+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode w+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1137)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1137)
- bool(true)
- -- File opened in mode x+ --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+b --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1024)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1024)
- bool(true)
- -- File opened in mode x+t --
- -- Reading beyond filesize, expected : 1024 bytes --
- int(0)
- bool(false)
- Reading 1030 bytes from file, expecting 1024 bytes ... OK
- int(1024)
- bool(true)
- string(32) "b09c8026a64a88d36d4c2f17983964bb"
- -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
- int(1137)
- bool(false)
- Reading 10 bytes from file, expecting 0 bytes ... OK
- int(1137)
- bool(true)
- Done
|