PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/ext/standard/tests/file/005_variation.php

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