2
Test fopen and fclose() functions - usage variations - "x+b" mode
7
Prototype: resource fopen(string $filename, string $mode
8
[, bool $use_include_path [, resource $context]] );
9
Description: Opens file or URL.
13
Prototype: bool fclose ( resource $handle );
14
Description: Closes an open file pointer
17
/* Test fopen() and fclose(): Opening the file in "x+b" mode,
18
checking for the file creation, write & read operations,
19
checking for the file pointer position,
20
checking for the warning msg when trying to open an existing file in "x+b" mode,
23
$file_path = dirname(__FILE__);
24
$string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
25
$file = $file_path."/007_variation24.tmp";
27
echo "*** Test fopen() & fclose() functions: with 'x+b' mode ***\n";
28
$file_handle = fopen($file, "x+b"); //opening the non-existing file in "x+b" mode, file will be created
29
var_dump($file_handle); //Check for the content of handle
30
var_dump( get_resource_type($file_handle) ); //Check for the type of resource
31
var_dump( ftell($file_handle) ); //Initial file pointer position, expected at the begining of the file
32
var_dump( fwrite($file_handle, $string) ); //Check for write operation; passes; expected:size of the $string
33
var_dump( ftell($file_handle) ); //File pointer position after write operation, expected at the end of the file
35
var_dump( fread($file_handle, 100) ); //Check for read operation; passes; expected: content of the file
36
var_dump( ftell($file_handle) ); //File pointer position after read operation, expected at the end of the file
37
var_dump( fclose($file_handle) ); //Check for close operation on the file handle
38
var_dump( get_resource_type($file_handle) ); //Check whether resource is lost after close operation
39
$file_handle = fopen($file, "x+b"); //Opening the existing data file in "x+b" mode to check for the warning message
40
echo "*** Done ***\n";
43
unlink(dirname(__FILE__)."/007_variation24.tmp");
46
*** Test fopen() & fclose() functions: with 'x+b' mode ***
47
resource(%d) of type (stream)
52
string(37) "abcdefghij
59
Warning: fopen(%s): failed to open stream: File exists in %s on line %d