1--TEST--
2Test fopen and fclose() functions - usage variations - "wt" mode
3--SKIPIF--
4<?php
5if( substr(PHP_OS, 0, 3) == "WIN" )
6  die('skip Do not run on Windows');
7?>
8--FILE--
9<?php
10
11/* Test fopen() and fclose(): Opening the file in "wt" mode,
12   checking for the file creation, write & read operations,
13   checking for the file pointer position,
14   checking for the file truncation when trying to open an existing file in "wt" mode,
15   and fclose function
16*/
17$file_path = __DIR__;
18require($file_path."/file.inc");
19
20create_files($file_path, 1, "text_with_new_line", 0755, 20, "wt", "007_variation", 11, "bytes");
21$file = $file_path."/007_variation11.tmp";
22$string = "abcdefghij\nmnopqrst\tuvwxyz\n0123456789";
23
24echo "*** Test fopen() & fclose() functions:  with 'wt' mode ***\n";
25$file_handle = fopen($file, "wt");  //opening the file "wt" mode
26var_dump($file_handle);  //Check for the content of handle
27var_dump( get_resource_type($file_handle) );  //Check for the type of resource
28var_dump( ftell($file_handle) );  //Initial file pointer position, expected at the beginning of the file
29var_dump( fwrite($file_handle, $string) );  //Check for write operation; passes; expected:size of the $string
30var_dump( ftell($file_handle) );  //File pointer position after write operation, expected at the end of the file
31rewind($file_handle);
32var_dump( fread($file_handle, 100) );  //Check for read operation; fails; expected: false
33var_dump( ftell($file_handle) );  //File pointer position after read operation, expected at the beginning of the file
34var_dump( fclose($file_handle) );  //Check for close operation on the file handle
35var_dump( get_resource_type($file_handle) );  //Check whether resource is lost after close operation
36
37var_dump( filesize($file) );  //Check for size of existing data file before opening the file in "wt" mode again, expected: size of content
38clearstatcache();
39fclose( fopen($file, "wt") );  //Opening the existing data file again in "wt" mode
40var_dump( filesize($file) );  //Check for size of existing data file after opening the file in "wt" mode again, expected: 0 bytes
41clearstatcache();
42
43unlink($file);  //Deleting the file
44fclose( fopen($file, "wt") );  //Opening the non-existing file in "wt" mode, which will be created
45var_dump( file_exists($file) );  //Check for the existence of file
46echo "*** Done ***\n";
47--CLEAN--
48<?php
49unlink(__DIR__."/007_variation11.tmp");
50?>
51--EXPECTF--
52*** Test fopen() & fclose() functions:  with 'wt' mode ***
53resource(%d) of type (stream)
54string(6) "stream"
55int(0)
56int(37)
57int(37)
58
59Notice: fread(): Read of 8192 bytes failed with errno=9 Bad file descriptor in %s on line %d
60bool(false)
61int(0)
62bool(true)
63string(7) "Unknown"
64int(37)
65int(0)
66bool(true)
67*** Done ***
68