/inc/func/directories.php

https://github.com/Implying/tsukiboards · PHP · 169 lines · 99 code · 16 blank · 54 comment · 29 complexity · 7257a6f401e0414a37201ebad47472ff MD5 · raw file

  1. <?php
  2. /**
  3. * Recursively delete a directory
  4. *
  5. * @param string $path Intial path to delete
  6. */
  7. function removeDir($path) {
  8. $normal_files = glob($path . "*");
  9. $hidden_files = glob($path . "\.?*");
  10. $all_files = array_merge($normal_files, $hidden_files);
  11. foreach ($all_files as $file) {
  12. /* Skip pseudo links to current and parent dirs (./ and ../). */
  13. if (preg_match("/(\.|\.\.)$/", $file))
  14. {
  15. continue;
  16. }
  17. if (is_file($file) === TRUE) {
  18. /* Remove each file in this Directory */
  19. unlink($file);
  20. echo _gettext('Removed File').': '. $file . "<br />";
  21. }
  22. else if (is_dir($file) === TRUE) {
  23. /* If this Directory contains a Subdirectory, run this Function on it */
  24. removeDir($file);
  25. }
  26. }
  27. /* Remove Directory once Files have been removed (If Exists) */
  28. if (is_dir($path) === TRUE) {
  29. rmdir($path);
  30. echo '<br />'. _gettext('Removed Directory').': ' . $path . "<br /><br />";
  31. }
  32. }
  33. /**
  34. * Remove a board
  35. *
  36. * @param string $dir Directory to remove
  37. * @return boolean Result
  38. */
  39. function removeBoard($dir){
  40. global $tc_db;
  41. if(!isset($GLOBALS['remerror'])) {
  42. $GLOBALS['remerror'] = false;
  43. }
  44. if($handle = opendir(KU_BOARDSDIR . $dir)){ /* If the folder exploration is sucsessful, continue */
  45. while (false !== ($file = readdir($handle))){ /* As long as storing the next file to $file is successful, continue */
  46. $path = $dir . '/' . $file;
  47. if(is_file(KU_BOARDSDIR . $path)){
  48. if(!unlink(KU_BOARDSDIR . $path)){
  49. echo '<u><font color="red">'.sprintf(_gettext('"%s" could not be deleted. This may be due to a permissions problem.</u><br />Directory cannot be deleted until all files are deleted.'), $path).'</font><br />';
  50. $GLOBALS['remerror'] = true;
  51. return false;
  52. }
  53. } else
  54. if(is_dir(KU_BOARDSDIR . $path) && substr($file, 0, 1) != '.'){
  55. removeBoard($path);
  56. @rmdir(KU_BOARDSDIR . $path);
  57. }
  58. }
  59. closedir($handle); /* Close the folder exploration */
  60. }
  61. if(!$GLOBALS['remerror']) /* If no errors occured, delete the now empty directory */
  62. if(!rmdir(KU_BOARDSDIR . $dir)){
  63. echo '<strong><font color="red">'.sprintf(_gettext('Could not remove directory "%s". This may be due to a permissions problem.'),$dir).'</font></strong><br />'.$GLOBALS['remerror'];
  64. return false;
  65. } else
  66. return true;
  67. return false;
  68. }
  69. /*
  70. ------------ lixlpixel recursive PHP functions -------------
  71. recursive_directory_size( directory, human readable format )
  72. expects path to directory and optional TRUE / FALSE
  73. PHP has to have the rights to read the directory you specify
  74. and all files and folders inside the directory to count size
  75. if you choose to get human readable format,
  76. the function returns the filesize in bytes, KB and MB
  77. ------------------------------------------------------------
  78. to use this function to get the filesize in bytes, write:
  79. recursive_directory_size('path/to/directory/to/count');
  80. to use this function to get the size in a nice format, write:
  81. recursive_directory_size('path/to/directory/to/count',TRUE);
  82. */
  83. /**
  84. * Find the size of a directory, including any subdirectories
  85. *
  86. * @param string $directory Directory
  87. * @param boolean $format Format
  88. * @return array Size/number of files
  89. */
  90. function recursive_directory_size($directory, $format=FALSE)
  91. {
  92. $size = 0;
  93. $files = 0;
  94. /* If the path has a slash at the end we remove it here */
  95. if(substr($directory,-1) == '/')
  96. {
  97. $directory = substr($directory,0,-1);
  98. }
  99. /* If the path is not valid or is not a directory ... */
  100. if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory))
  101. {
  102. /* ... We return -1 and exit the function */
  103. return -1;
  104. }
  105. /* We open the directory */
  106. if($handle = opendir($directory))
  107. {
  108. /* And scan through the items inside */
  109. while(($file = readdir($handle)) !== false)
  110. {
  111. /* We build the new path */
  112. $path = $directory.'/'.$file;
  113. /* If the filepointer is not the current directory or the parent directory */
  114. if($file != '.' && $file != '..')
  115. {
  116. /* If the new path is a file */
  117. if(is_file($path))
  118. {
  119. /* We add the filesize to the total size */
  120. $size += filesize($path);
  121. $files++;
  122. /* If the new path is a directory */
  123. }elseif(is_dir($path))
  124. {
  125. /* If $format is set to true, follow the directory and recalculate from there */
  126. if ($format) {
  127. /* We call this function with the new path */
  128. $handlesize = recursive_directory_size($path);
  129. if (is_int($handlesize)) {
  130. /* If the function returns more than zero */
  131. if($handlesize >= 0)
  132. {
  133. /* We add the result to the total size */
  134. $size += $handlesize;
  135. /* Else we return -1 and exit the function */
  136. }else{
  137. return -1;
  138. }
  139. /* Else we return -1 and exit the function */
  140. }else{
  141. return -1;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. /* Close the directory */
  148. closedir($handle);
  149. }
  150. /* Return the total filesize in bytes */
  151. return array($size,$files);
  152. }
  153. ?>