1--TEST--
2Test fopen and fclose() functions - usage variations - "xt" mode
3--FILE--
4<?php
5/*
6 fopen() function:
7 Prototype: resource fopen(string $filename, string $mode
8                            [, bool $use_include_path [, resource $context]] );
9 Description: Opens file or URL.
10*/
11/*
12 fclose() function:
13 Prototype: bool fclose ( resource $handle );
14 Description: Closes an open file pointer
15*/
16
17/* Test fopen() and fclose(): Opening the file in "xt" 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 "xt" mode,
21   and fclose function
22*/
23$file_path = dirname(__FILE__);
24$string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
25$file = $file_path."/007_variation15.tmp";
26
27echo "*** Test fopen() & fclose() functions:  with 'xt' mode ***\n";
28$file_handle = fopen($file, "xt");  //opening the non-existing file in "xt" mode, file will be created
29var_dump($file_handle);  //Check for the content of handle
30var_dump( get_resource_type($file_handle) );  //Check for the type of resource
31var_dump( ftell($file_handle) );  //Initial file pointer position, expected at the beginning of the file
32var_dump( fwrite($file_handle, $string) );  //Check for write operation; passes; expected:size of the $string
33var_dump( ftell($file_handle) );  //File pointer position after write operation, expected at the end of the file
34rewind($file_handle);
35var_dump( fread($file_handle, 100) );  //Check for read operation; fails; expected: empty string
36var_dump( ftell($file_handle) );  //File pointer position after read operation, expected at the beginning of the file
37var_dump( fclose($file_handle) );  //Check for close operation on the file handle
38var_dump( get_resource_type($file_handle) );  //Check whether resource is lost after close operation
39$file_handle = fopen($file, "xt");  //Opening the existing data file in 'xt' mode to check for the warning message
40echo "*** Done ***\n";
41--CLEAN--
42<?php
43unlink(dirname(__FILE__)."/007_variation15.tmp");
44?>
45--EXPECTF--
46*** Test fopen() & fclose() functions:  with 'xt' mode ***
47resource(%d) of type (stream)
48string(6) "stream"
49int(0)
50int(37)
51int(37)
52string(0) ""
53int(0)
54bool(true)
55string(7) "Unknown"
56
57Warning: fopen(%s): failed to open stream: File exists in %s on line %d
58*** Done ***
59