1--TEST--
2Test fileatime(), filemtime(), filectime() & touch() functions : usage variation
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) != 'WIN') {
6    die('skip Windows-only test');
7}
8?>
9--FILE--
10<?php
11
12function stat_fn( $filename ) {
13  echo "-- File access time is => ";
14  print( @date( 'Y:M:D:H:i:s', fileatime($filename) ) )."\n";
15  clearstatcache();
16  echo "-- File modification time is => ";
17  print( @date( 'Y:M:D:H:i:s', filemtime($filename) ) )."\n";
18  clearstatcache();
19  echo "-- inode change time is => ";
20  print( @date( 'Y:M:D:H:i:s', filectime($filename) ) )."\n";
21  clearstatcache();
22}
23
24echo "*** Testing fileattime(), filemtime(), filectime() & touch() : usage variations ***\n";
25$file_path = __DIR__;
26// create files
27$file_handle = fopen("$file_path/005_variation1.tmp", "w");
28fclose($file_handle);
29stat_fn("$file_path/005_variation1.tmp");
30sleep(1);
31
32$file_handle = fopen("$file_path/005_variation2.tmp", "w");
33fclose($file_handle);
34stat_fn("$file_path/005_variation2.tmp");
35sleep(1);
36
37$file_handle = fopen("$file_path/005_variation3.tmp", "w");
38fclose($file_handle);
39stat_fn("$file_path/005_variation3.tmp");
40
41// delete files
42unlink("$file_path/005_variation1.tmp");
43unlink("$file_path/005_variation2.tmp");
44unlink("$file_path/005_variation3.tmp");
45
46echo "\n-- Checking different times, just after creating the file --\n";
47$file_name = "$file_path/005_variation1.tmp";
48$file_write_handle = fopen($file_name, "w");
49fclose($file_write_handle);
50stat_fn($file_name);
51sleep(1);
52
53/* filectime + 2 */
54echo "\n-- Checking different times, after changing the file permission --\n";
55chmod($file_name, 0777);
56stat_fn($file_name);
57sleep(1);
58
59/* filemtime + 2 & filectime + 2 */
60echo "\n-- Checking different times, after writing into the file --\n";
61$file_write_handle = fopen($file_name, "w");
62fwrite($file_write_handle, "Hello, world");
63fclose($file_write_handle);
64stat_fn($file_name);
65sleep(1);
66
67/* fileatime + 2 */
68echo "\n-- Checking different times, after reading from the file --\n";
69$file_read_handle = fopen($file_name ,"r");
70fread($file_read_handle, 10);
71fclose( $file_read_handle);
72stat_fn($file_name);
73sleep(1);
74
75/* No change */
76echo "\n-- Checking same times, after creating a softlink to the file --\n";
77symlink($file_name, "$file_path/005_variation_softlink.tmp");
78stat_fn($file_name);
79sleep(1);
80
81/* filectime + 2 */
82echo "\n-- Checking different times, after creating a hardlink to the file --\n";
83link($file_name, "$file_path/005_variation_hardlink.tmp");
84stat_fn($file_name);
85sleep(1);
86
87/* No change */
88echo "\n-- Checking same times, after making a copy of the file --\n";
89$file_copy = "$file_path/005_variation_copy.tmp";
90copy($file_name, $file_copy);
91stat_fn($file_name);
92sleep(1);
93
94/* fileatime + 2 */
95echo "\n-- Checking different times, after performing is_file() operation on the file --\n";
96is_file($file_name);
97stat_fn($file_name);
98sleep(1);
99
100
101echo "\n*** Testing touch() function with different time values ***\n";
102$file_name2 = $file_path."/005_variation_touch.tmp";
103$file_handle = fopen($file_name2, "w");
104fclose($file_handle);
105sleep(1);
106
107/* Time is not mentioned */
108var_dump( touch($file_name2) ); //set to current system time
109stat_fn($file_name2);
110sleep(1);
111
112/* set to access(creation time of the file) time */
113var_dump( touch($file_name2, @date(fileatime($file_name2))) );
114stat_fn($file_name2);
115sleep(1);
116
117/* set to access time of $file_name2 */
118var_dump( touch($file_path."/005_variation_touch_fly.tmp", @date(fileatime($file_name2)), time()) );
119stat_fn($file_name2);
120sleep(1);
121
122/* set to default value, with Invalid timestamps */
123var_dump( touch($file_name2, 10) );
124stat_fn($file_name2);
125var_dump( touch($file_name2, 10, 20) );
126stat_fn($file_name2);
127
128/* touch() after renaming the file */
129rename($file_name2, "$file_path/005_variation_touch_new.tmp");
130stat_fn("$file_path/005_variation_touch_new.tmp");
131
132echo "Done\n";
133?>
134--CLEAN--
135<?php
136$file_path = __DIR__;
137unlink($file_path."/005_variation_softlink.tmp");
138unlink($file_path."/005_variation_hardlink.tmp");
139unlink($file_path."/005_variation1.tmp");
140unlink($file_path."/005_variation_copy.tmp");
141unlink($file_path."/005_variation_touch.tmp");
142unlink($file_path."/005_variation_touch_fly.tmp");
143unlink($file_path."/005_variation_touch_new.tmp");
144?>
145--EXPECTF--
146*** Testing fileattime(), filemtime(), filectime() & touch() : usage variations ***
147-- File access time is => %d:%s:%s:%d:%d:%d
148-- File modification time is => %d:%s:%s:%d:%d:%d
149-- inode change time is => %d:%s:%s:%d:%d:%d
150-- File access time is => %d:%s:%s:%d:%d:%d
151-- File modification time is => %d:%s:%s:%d:%d:%d
152-- inode change time is => %d:%s:%s:%d:%d:%d
153-- File access time is => %d:%s:%s:%d:%d:%d
154-- File modification time is => %d:%s:%s:%d:%d:%d
155-- inode change time is => %d:%s:%s:%d:%d:%d
156
157-- Checking different times, just after creating the file --
158-- File access time is => %d:%s:%s:%d:%d:%d
159-- File modification time is => %d:%s:%s:%d:%d:%d
160-- inode change time is => %d:%s:%s:%d:%d:%d
161
162-- Checking different times, after changing the file permission --
163-- File access time is => %d:%s:%s:%d:%d:%d
164-- File modification time is => %d:%s:%s:%d:%d:%d
165-- inode change time is => %d:%s:%s:%d:%d:%d
166
167-- Checking different times, after writing into the file --
168-- File access time is => %d:%s:%s:%d:%d:%d
169-- File modification time is => %d:%s:%s:%d:%d:%d
170-- inode change time is => %d:%s:%s:%d:%d:%d
171
172-- Checking different times, after reading from the file --
173-- File access time is => %d:%s:%s:%d:%d:%d
174-- File modification time is => %d:%s:%s:%d:%d:%d
175-- inode change time is => %d:%s:%s:%d:%d:%d
176
177-- Checking same times, after creating a softlink to the file --
178-- File access time is => %d:%s:%s:%d:%d:%d
179-- File modification time is => %d:%s:%s:%d:%d:%d
180-- inode change time is => %d:%s:%s:%d:%d:%d
181
182-- Checking different times, after creating a hardlink to the file --
183-- File access time is => %d:%s:%s:%d:%d:%d
184-- File modification time is => %d:%s:%s:%d:%d:%d
185-- inode change time is => %d:%s:%s:%d:%d:%d
186
187-- Checking same times, after making a copy of the file --
188-- File access time is => %d:%s:%s:%d:%d:%d
189-- File modification time is => %d:%s:%s:%d:%d:%d
190-- inode change time is => %d:%s:%s:%d:%d:%d
191
192-- Checking different times, after performing is_file() operation on the file --
193-- File access time is => %d:%s:%s:%d:%d:%d
194-- File modification time is => %d:%s:%s:%d:%d:%d
195-- inode change time is => %d:%s:%s:%d:%d:%d
196
197*** Testing touch() function with different time values ***
198bool(true)
199-- File access time is => %d:%s:%s:%d:%d:%d
200-- File modification time is => %d:%s:%s:%d:%d:%d
201-- inode change time is => %d:%s:%s:%d:%d:%d
202bool(true)
203-- File access time is => %d:%s:%s:%d:%d:%d
204-- File modification time is => %d:%s:%s:%d:%d:%d
205-- inode change time is => %d:%s:%s:%d:%d:%d
206bool(true)
207-- File access time is => %d:%s:%s:%d:%d:%d
208-- File modification time is => %d:%s:%s:%d:%d:%d
209-- inode change time is => %d:%s:%s:%d:%d:%d
210bool(true)
211-- File access time is => %d:%s:%s:%d:%d:%d
212-- File modification time is => %d:%s:%s:%d:%d:%d
213-- inode change time is => %d:%s:%s:%d:%d:%d
214bool(true)
215-- File access time is => %d:%s:%s:%d:%d:%d
216-- File modification time is => %d:%s:%s:%d:%d:%d
217-- inode change time is => %d:%s:%s:%d:%d:%d
218-- File access time is => %d:%s:%s:%d:%d:%d
219-- File modification time is => %d:%s:%s:%d:%d:%d
220-- inode change time is => %d:%s:%s:%d:%d:%d
221Done
222