005_variation.phpt 8.6 KB

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