FolderLib.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. App::uses('Folder', 'Utility');
  3. /**
  4. * Folder lib with a few custom improvements
  5. * 2010-05-16 ms
  6. */
  7. class FolderLib extends Folder {
  8. /**
  9. * Empty the folder.
  10. * Instead of deleting the folder itself as delete() does,
  11. * this method only removes its content recursivly.
  12. *
  13. * Note: It skips hidden folders (starting with a . dot).
  14. *
  15. * @return boolean Success or null on invalid folder
  16. * 2010-12-07 ms
  17. */
  18. public function clear($path = null) {
  19. if (!$path) {
  20. $path = $this->pwd();
  21. }
  22. if (!$path) {
  23. return null;
  24. }
  25. $path = Folder::slashTerm($path);
  26. if (!is_dir($path)) {
  27. return null;
  28. }
  29. $normalFiles = glob($path . '*');
  30. $hiddenFiles = glob($path . '\.?*');
  31. $normalFiles = $normalFiles ? $normalFiles : array();
  32. $hiddenFiles = $hiddenFiles ? $hiddenFiles : array();
  33. $files = array_merge($normalFiles, $hiddenFiles);
  34. foreach ($files as $file) {
  35. if (preg_match('/(\.|\.\.)$/', $file)) {
  36. continue;
  37. }
  38. if (is_file($file)) {
  39. if (@unlink($file)) {
  40. $this->_messages[] = __('%s removed', $file);
  41. } else {
  42. $this->_errors[] = __('%s NOT removed', $file);
  43. }
  44. } elseif (is_dir($file) && $this->delete($file) === false) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. }