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/*
12   Prototype: int fileatime ( string $filename );
13   Description: Returns the time the file was last accessed, or FALSE
14     in case of an error. The time is returned as a Unix timestamp.
15
16   Prototype: int filemtime ( string $filename );
17   Description: Returns the time the file was last modified, or FALSE
18     in case of an error.
19
20   Prototype: int filectime ( string $filename );
21   Description: Returns the time the file was last changed, or FALSE
22     in case of an error. The time is returned as a Unix timestamp.
23
24   Prototype: bool touch ( string $filename [, int $time [, int $atime]] );
25   Description: Attempts to set the access and modification times of the file
26     named in the filename parameter to the value given in time.
27*/
28
29/*
30   Prototype: void stat_fn(string $filename);
31   Description: Prints access, modification and change times of a file
32*/
33function stat_fn( $filename ) {
34  echo "-- File access time is => ";
35  print( @date( 'Y:M:D:H:i:s', fileatime($filename) ) )."\n";
36  clearstatcache();
37  echo "-- File modification time is => ";
38  print( @date( 'Y:M:D:H:i:s', filemtime($filename) ) )."\n";
39  clearstatcache();
40  echo "-- inode change time is => ";
41  print( @date( 'Y:M:D:H:i:s', filectime($filename) ) )."\n";
42  clearstatcache();
43
44}
45
46echo "*** Testing fileattime(), filemtime(), filectime() & touch() : usage variations ***\n";
47$file_path = dirname(__FILE__);
48// create files
49$file_handle = fopen("$file_path/005_variation1.tmp", "w");
50fclose($file_handle);
51stat_fn("$file_path/005_variation1.tmp");
52sleep(2);
53
54$file_handle = fopen("$file_path/005_variation2.tmp", "w");
55fclose($file_handle);
56stat_fn("$file_path/005_variation2.tmp");
57sleep(2);
58
59$file_handle = fopen("$file_path/005_variation3.tmp", "w");
60fclose($file_handle);
61stat_fn("$file_path/005_variation3.tmp");
62
63// delete files
64unlink("$file_path/005_variation1.tmp");
65unlink("$file_path/005_variation2.tmp");
66unlink("$file_path/005_variation3.tmp");
67
68echo "\n-- Checking different times, just after creating the file --\n";
69$file_name = "$file_path/005_variation1.tmp";
70$file_write_handle = fopen($file_name, "w");
71fclose($file_write_handle);
72stat_fn($file_name);
73sleep(2);
74
75/* filectime + 2 */
76echo "\n-- Checking different times, after changing the file permission --\n";
77chmod($file_name, 0777);
78stat_fn($file_name);
79sleep(2);
80
81/* filemtime + 2 & filectime + 2 */
82echo "\n-- Checking different times, after writing into the file --\n";
83$file_write_handle = fopen($file_name, "w");
84fwrite($file_write_handle, b"Hello, world");
85fclose($file_write_handle);
86stat_fn($file_name);
87sleep(2);
88
89/* fileatime + 2 */
90echo "\n-- Checking different times, after reading from the file --\n";
91$file_read_handle = fopen($file_name ,"r");
92fread($file_read_handle, 10);
93fclose( $file_read_handle);
94stat_fn($file_name);
95sleep(2);
96
97/* No change */
98echo "\n-- Checking different times, after making a copy of the file --\n";
99$file_copy = "$file_path/005_variation_copy.tmp";
100copy($file_name, $file_copy);
101stat_fn($file_name);
102sleep(2);
103
104/* fileatime + 2 */
105echo "\n-- Checking different times, after performing is_file() operation on the file --\n";
106is_file($file_name);
107stat_fn($file_name);
108sleep(2);
109
110
111echo "\n*** Testing touch() function with different time values ***\n";
112$file_name2 = $file_path."/005_variation_touch.tmp";
113$file_handle = fopen($file_name2, "w");
114fclose($file_handle);
115sleep(2);
116
117/* Time is not mentioned */
118var_dump( touch($file_name2) ); //set to current system time
119stat_fn($file_name2);
120sleep(2);
121
122/* set to access(creation time of the file) time */
123var_dump( touch($file_name2, @date(fileatime($file_name2))) );
124stat_fn($file_name2);
125sleep(2);
126
127/* set to access time of $file_name2 */
128var_dump( touch($file_path."/005_variation_touch_fly.tmp", @date(fileatime($file_name2)), time()) );
129stat_fn($file_name2);
130sleep(2);
131
132/* set to default value, with Invalid timestamps */
133var_dump( touch($file_name2, 10) );
134stat_fn($file_name2);
135var_dump( touch($file_name2, 10, 20) );
136stat_fn($file_name2);
137
138/* touch() after renaming the file */
139rename($file_name2, "$file_path/005_variation_touch_new.tmp");
140stat_fn("$file_path/005_variation_touch_new.tmp");
141
142echo "Done\n";
143?>
144--CLEAN--
145<?php
146$file_path = dirname(__FILE__);
147unlink($file_path."/005_variation_softlink.tmp");
148unlink($file_path."/005_variation_hardlink.tmp");
149unlink($file_path."/005_variation1.tmp");
150unlink($file_path."/005_variation_copy.tmp");
151unlink($file_path."/005_variation_touch.tmp");
152unlink($file_path."/005_variation_touch_fly.tmp");
153unlink($file_path."/005_variation_touch_new.tmp");
154?>
155--EXPECTF--
156*** Testing fileattime(), filemtime(), filectime() & touch() : usage variations ***
157-- File access time is => %d:%s:%s:%d:%d:%d
158-- File modification time is => %d:%s:%s:%d:%d:%d
159-- inode change time is => %d:%s:%s:%d:%d:%d
160-- File access time is => %d:%s:%s:%d:%d:%d
161-- File modification time is => %d:%s:%s:%d:%d:%d
162-- inode change time is => %d:%s:%s:%d:%d:%d
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, just after creating 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 changing the file permission --
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 different times, after writing into 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 reading from 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 different 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