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