PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/Gelato/FileSystem/Dir.php

https://github.com/ravilrrr/monstra-cms
PHP | 206 lines | 63 code | 30 blank | 113 comment | 10 complexity | 310e3e9c7871579daea9653c64454186 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Gelato Library
  4. *
  5. * This source file is part of the Gelato Library. More information,
  6. * documentation and tutorials can be found at http://gelato.monstra.org
  7. *
  8. * @package Gelato
  9. *
  10. * @author Romanenko Sergey / Awilum <awilum@msn.com>
  11. * @copyright 2012-2014 Romanenko Sergey / Awilum <awilum@msn.com>
  12. *
  13. * For the full copyright and license information, please view the LICENSE
  14. * file that was distributed with this source code.
  15. */
  16. class Dir
  17. {
  18. /**
  19. * Protected constructor since this is a static class.
  20. *
  21. * @access protected
  22. */
  23. protected function __construct()
  24. {
  25. // Nothing here
  26. }
  27. /**
  28. * Creates a directory
  29. *
  30. * <code>
  31. * Dir::create('folder1');
  32. * </code>
  33. *
  34. * @param string $dir Name of directory to create
  35. * @param integer $chmod Chmod
  36. * @return boolean
  37. */
  38. public static function create($dir, $chmod = 0775)
  39. {
  40. // Redefine vars
  41. $dir = (string) $dir;
  42. // Create new dir if $dir !exists
  43. return ( ! Dir::exists($dir)) ? @mkdir($dir, $chmod, true) : true;
  44. }
  45. /**
  46. * Checks if this directory exists.
  47. *
  48. * <code>
  49. * if (Dir::exists('folder1')) {
  50. * // Do something...
  51. * }
  52. * </code>
  53. *
  54. * @param string $dir Full path of the directory to check.
  55. * @return boolean
  56. */
  57. public static function exists($dir)
  58. {
  59. // Redefine vars
  60. $dir = (string) $dir;
  61. // Directory exists
  62. if (file_exists($dir) && is_dir($dir)) return true;
  63. // Doesn't exist
  64. return false;
  65. }
  66. /**
  67. * Check dir permission
  68. *
  69. * <code>
  70. * echo Dir::checkPerm('folder1');
  71. * </code>
  72. *
  73. * @param string $dir Directory to check
  74. * @return string
  75. */
  76. public static function checkPerm($dir)
  77. {
  78. // Redefine vars
  79. $dir = (string) $dir;
  80. // Clear stat cache
  81. clearstatcache();
  82. // Return perm
  83. return substr(sprintf('%o', fileperms($dir)), -4);
  84. }
  85. /**
  86. * Delete directory
  87. *
  88. * <code>
  89. * Dir::delete('folder1');
  90. * </code>
  91. *
  92. * @param string $dir Name of directory to delete
  93. */
  94. public static function delete($dir)
  95. {
  96. // Redefine vars
  97. $dir = (string) $dir;
  98. // Delete dir
  99. if (is_dir($dir)){$ob=scandir($dir);foreach ($ob as $o) {if ($o!='.'&&$o!='..') {if(filetype($dir.'/'.$o)=='dir')Dir::delete($dir.'/'.$o); else unlink($dir.'/'.$o);}}}
  100. reset($ob); rmdir($dir);
  101. }
  102. /**
  103. * Get list of directories
  104. *
  105. * <code>
  106. * $dirs = Dir::scan('folders');
  107. * </code>
  108. *
  109. * @param string $dir Directory
  110. */
  111. public static function scan($dir)
  112. {
  113. // Redefine vars
  114. $dir = (string) $dir;
  115. // Scan dir
  116. if (is_dir($dir)&&$dh=opendir($dir)){$f=array();while ($fn=readdir($dh)) {if($fn!='.'&&$fn!='..'&&is_dir($dir.DS.$fn))$f[]=$fn;}return$f;}
  117. }
  118. /**
  119. * Check if a directory is writable.
  120. *
  121. * <code>
  122. * if (Dir::writable('folder1')) {
  123. * // Do something...
  124. * }
  125. * </code>
  126. *
  127. * @param string $path The path to check.
  128. * @return booleans
  129. */
  130. public static function writable($path)
  131. {
  132. // Redefine vars
  133. $path = (string) $path;
  134. // Create temporary file
  135. $file = tempnam($path, 'writable');
  136. // File has been created
  137. if ($file !== false) {
  138. // Remove temporary file
  139. File::delete($file);
  140. // Writable
  141. return true;
  142. }
  143. // Else not writable
  144. return false;
  145. }
  146. /**
  147. * Get directory size.
  148. *
  149. * <code>
  150. * echo Dir::size('folder1');
  151. * </code>
  152. *
  153. * @param string $path The path to directory.
  154. * @return integer
  155. */
  156. public static function size($path)
  157. {
  158. // Redefine vars
  159. $path = (string) $path;
  160. $total_size = 0;
  161. $files = scandir($path);
  162. $clean_path = rtrim($path, '/') . '/';
  163. foreach ($files as $t) {
  164. if ($t <> "." && $t <> "..") {
  165. $current_file = $clean_path . $t;
  166. if (is_dir($current_file)) {
  167. $total_size += Dir::size($current_file);
  168. } else {
  169. $total_size += filesize($current_file);
  170. }
  171. }
  172. }
  173. // Return total size
  174. return $total_size;
  175. }
  176. }