1--TEST--
2Test fopen and fclose() functions - usage variations - "x+" mode
3--FILE--
4<?php
5
6/* Test fopen() and fclose(): Opening the file in "x+" 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 "x+" mode,
10   and fclose function
11*/
12$file_path = __DIR__;
13$string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
14$file = $file_path."/007_variation8.tmp";
15
16echo "*** Test fopen() & fclose() functions:  with 'x+' mode ***\n";
17$file_handle = fopen($file, "x+");  //opening the non-existing file in "x+" 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; passes; expected: content of the file
25var_dump( ftell($file_handle) );  //File pointer position after read operation, expected at the end 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, "x+");  //Opening the existing data file in "x+" mode to check for the warning message
29echo "*** Done ***\n";
30?>
31--CLEAN--
32<?php
33unlink(__DIR__."/007_variation8.tmp");
34?>
35--EXPECTF--
36*** Test fopen() & fclose() functions:  with 'x+' mode ***
37resource(%d) of type (stream)
38string(6) "stream"
39int(0)
40int(37)
41int(37)
42string(37) "abcdefghij
43mnopqrst	uvwxyz
440123456789"
45int(37)
46bool(true)
47string(7) "Unknown"
48
49Warning: fopen(%s): Failed to open stream: File exists in %s on line %d
50*** Done ***
51