Storage.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\common\services;
  3. class Storage
  4. {
  5. /**
  6. * Read a file and return bin data
  7. *
  8. * @param string $filename
  9. * @return bool|string
  10. * @throws \Exception
  11. */
  12. public static function get($filename)
  13. {
  14. $result = file_get_contents($filename, 'r');
  15. if (false === $result) {
  16. throw new \Exception("Failed to read $filename.");
  17. }
  18. return $result;
  19. }
  20. public static function put($filename, $data)
  21. {
  22. return file_put_contents($filename, $data);
  23. }
  24. public static function exists($filename)
  25. {
  26. return file_exists($filename);
  27. }
  28. public static function hash($filename, $type = 'sha256')
  29. {
  30. return hash_file('sha256', $filename);
  31. }
  32. public static function rename($fname, $new_fname)
  33. {
  34. if (false === rename($fname, $new_fname)) {
  35. throw new \Exception("Failed to rename $fname to $new_fname.");
  36. }
  37. return $new_fname;
  38. }
  39. public static function size($filename)
  40. {
  41. if (self::exists($filename)) {
  42. return filesize($filename);
  43. } else {
  44. return 0;
  45. }
  46. }
  47. /**
  48. * Remove a file
  49. *
  50. * @param $filename
  51. * @return $bool
  52. */
  53. public static function remove($filename)
  54. {
  55. if (self::exists($filename)) {
  56. return unlink($filename);
  57. }
  58. }
  59. public static function removeDir($dir)
  60. {
  61. $resource = opendir($dir);
  62. $size = 0;
  63. while($filename = @readdir($resource)) {
  64. if ($filename != "." && $filename != "..") {
  65. $path = "$dir/$filename";
  66. if (is_dir($path)) {
  67. // recursion
  68. self::removeDir($path."/");
  69. } else {
  70. unlink($path);
  71. }
  72. }
  73. }
  74. closedir($resource);
  75. return rmdir($dir);
  76. }
  77. /**
  78. * Recursively count the size of specified directory
  79. *
  80. * @param string $dir
  81. * @return int, total size in bytes
  82. */
  83. public static function getDirSize($dir)
  84. {
  85. $resource = opendir($dir);
  86. $size = 0;
  87. while($filename = @readdir($resource)) {
  88. if ($filename != "." && $filename != "..") {
  89. $path = "$dir/$filename";
  90. if (is_dir($path)) {
  91. // recursion
  92. $size += self::getDirSize($path);
  93. } else if (is_file($path)) {
  94. $size += filesize($path);
  95. }
  96. }
  97. }
  98. closedir($resource);
  99. return $size;
  100. }
  101. /**
  102. * Recursively count files of specified directory
  103. *
  104. * @param string $dir
  105. * @param $file_num
  106. * @return int, total size in bytes
  107. */
  108. public static function getFileNum($dir, $file_num = 0)
  109. {
  110. $resource = opendir($dir);
  111. while($filename = readdir($resource)) {
  112. if ($filename != "." && $filename != "..") {
  113. $path = "$dir/$filename";
  114. if (is_dir($path)) {
  115. // recursion
  116. $file_num = self::getFileNum($path, $file_num);
  117. } else {
  118. $file_num++;
  119. }
  120. }
  121. }
  122. closedir($resource);
  123. return $file_num;
  124. }
  125. /**
  126. * Copy directory recursively
  127. *
  128. * @param string $source
  129. * @param string $dest
  130. * @return bool
  131. */
  132. public static function copyDir($source, $dest)
  133. {
  134. if(!is_dir($source))
  135. return false;
  136. if(!is_dir($dest))
  137. mkdir($dest, 0777, true);
  138. $handle = dir($source);
  139. while($entry = $handle->read()) {
  140. if ($entry != "." && $entry != "..") {
  141. if (is_dir($source.'/'.$entry)) {
  142. // recursion
  143. self::copyDir($source.'/'.$entry, $dest.'/'.$entry);
  144. } else {
  145. @copy($source.'/'.$entry, $dest.'/'.$entry);
  146. }
  147. }
  148. }
  149. return true;
  150. }
  151. }