1--TEST-- 2Test fopen and fclose() functions - usage variations - "wb" mode 3--FILE-- 4<?php 5 6/* Test fopen() and fclose(): Opening the file in "wb" mode, 7 checking for the file creation, write & read operations, 8 checking for the file pointer position, 9 checking for the file truncation when trying to open an existing file in "wb" mode, 10 and fclose function 11*/ 12$file_path = __DIR__; 13require($file_path."/file.inc"); 14 15create_files($file_path, 1, "text_with_new_line", 0755, 20, "wb", "007_variation", 19, "bytes"); 16$file = $file_path."/007_variation19.tmp"; 17$string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789"; 18 19echo "*** Test fopen() & fclose() functions: with 'wb' mode ***\n"; 20$file_handle = fopen($file, "wb"); //opening the file "wb" mode 21var_dump($file_handle); //Check for the content of handle 22var_dump( get_resource_type($file_handle) ); //Check for the type of resource 23var_dump( ftell($file_handle) ); //Initial file pointer position, expected at the beginning of the file 24var_dump( fwrite($file_handle, $string) ); //Check for write operation; passes; expected:size of the $string 25var_dump( ftell($file_handle) ); //File pointer position after write operation, expected at the end of the file 26rewind($file_handle); 27var_dump( fread($file_handle, 100) ); //Check for read operation; fails; expected: false 28var_dump( ftell($file_handle) ); //File pointer position after read operation, expected at the beginning of the file 29var_dump( fclose($file_handle) ); //Check for close operation on the file handle 30var_dump( get_resource_type($file_handle) ); //Check whether resource is lost after close operation 31 32var_dump( filesize($file) ); //Check for size of existing data file before opening the file in "wb" mode again, expected: size of content 33clearstatcache(); 34fclose( fopen($file, "wb") ); //Opening the existing data file again in "wb" mode 35var_dump( filesize($file) ); //Check for size of existing data file after opening the file in "wb" mode again, expected: 0 bytes 36clearstatcache(); 37 38unlink($file); //Deleting the file 39fclose( fopen($file, "wb") ); //Opening the non-existing file in "wb" mode, which will be created 40var_dump( file_exists($file) ); //Check for the existence of file 41echo "*** Done ***\n"; 42--CLEAN-- 43<?php 44unlink(__DIR__."/007_variation19.tmp"); 45?> 46--EXPECTF-- 47*** Test fopen() & fclose() functions: with 'wb' mode *** 48resource(%d) of type (stream) 49string(6) "stream" 50int(0) 51int(37) 52int(37) 53 54Notice: fread(): Read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d 55bool(false) 56int(0) 57bool(true) 58string(7) "Unknown" 59int(37) 60int(0) 61bool(true) 62*** Done *** 63