1--TEST-- 2Test fopen and fclose() functions - usage variations - "w" mode 3--FILE-- 4<?php 5 6/* Test fopen() and fclose(): Opening the file in "w" 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 "w" 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, "w", "007_variation", 3, "bytes"); 16$file = $file_path."/007_variation3.tmp"; 17$string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789"; 18 19echo "*** Test fopen() & fclose() functions: with 'w' mode ***\n"; 20$file_handle = fopen($file, "w"); //opening the file "w" 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 "w" mode again, expected: size of content 33clearstatcache(); 34fclose( fopen($file, "w") ); //Opening the existing data file again in "w" mode 35var_dump( filesize($file) ); //Check for size of existing data file after opening the file in "w" mode again, expected: 0 bytes 36clearstatcache(); 37 38unlink($file); //Deleting the file 39fclose( fopen($file, "w") ); //Opening the non-existing file in "w" mode, which will be created 40var_dump( file_exists($file) ); //Check for the existence of file 41echo "*** Done ***\n"; 42?> 43--CLEAN-- 44<?php 45unlink(__DIR__."/007_variation3.tmp"); 46?> 47--EXPECTF-- 48*** Test fopen() & fclose() functions: with 'w' mode *** 49resource(%d) of type (stream) 50string(6) "stream" 51int(0) 52int(37) 53int(37) 54 55Notice: fread(): Read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d 56bool(false) 57int(0) 58bool(true) 59string(7) "Unknown" 60int(37) 61int(0) 62bool(true) 63*** Done *** 64