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", 4, "bytes");
16$file = $file_path."/007_variation4.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; passes; expected: content of file
28var_dump( ftell($file_handle) );  //File pointer position after read operation, expected at the end 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--CLEAN--
43<?php
44unlink(__DIR__."/007_variation4.tmp");
45?>
46--EXPECTF--
47*** Test fopen() & fclose() functions:  with 'w+' mode ***
48resource(%d) of type (stream)
49string(6) "stream"
50int(0)
51int(37)
52int(37)
53string(37) "abcdefghij
54mnopqrst	uvwxyz
550123456789"
56int(37)
57bool(true)
58string(7) "Unknown"
59int(37)
60int(0)
61bool(true)
62*** Done ***
63