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_variation7.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; 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, "x");  //Opening the existing data file in 'x' mode to check for the warning message
29echo "*** Done ***\n";
30--CLEAN--
31<?php
32unlink(__DIR__."/007_variation7.tmp");
33?>
34--EXPECTF--
35*** Test fopen() & fclose() functions:  with 'x' mode ***
36resource(%d) of type (stream)
37string(6) "stream"
38int(0)
39int(37)
40int(37)
41
42Notice: fread(): Read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
43bool(false)
44int(0)
45bool(true)
46string(7) "Unknown"
47
48Warning: fopen(%s): Failed to open stream: File exists in %s on line %d
49*** Done ***
50