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