FolderLib.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. * @return boolean Success or null on invalid folder
  14. * 2010-12-07 ms
  15. */
  16. public function clear($path = null) {
  17. if (!$path) {
  18. $path = $this->pwd();
  19. }
  20. if (!$path) {
  21. return null;
  22. }
  23. $path = Folder::slashTerm($path);
  24. if (is_dir($path)) {
  25. $normalFiles = glob($path . '*');
  26. $hiddenFiles = glob($path . '\.?*');
  27. $normalFiles = $normalFiles ? $normalFiles : array();
  28. $hiddenFiles = $hiddenFiles ? $hiddenFiles : array();
  29. $files = array_merge($normalFiles, $hiddenFiles);
  30. if (is_array($files)) {
  31. foreach ($files as $file) {
  32. if (preg_match('/(\.|\.\.)$/', $file)) {
  33. continue;
  34. }
  35. chmod($file, 0770);
  36. if (is_file($file)) {
  37. if (@unlink($file)) {
  38. $this->_messages[] = __('%s removed', $file);
  39. } else {
  40. $this->_errors[] = __('%s NOT removed', $file);
  41. }
  42. } elseif (is_dir($file) && $this->delete($file) === false) {
  43. return false;
  44. }
  45. }
  46. }
  47. } else {
  48. unlink($path);
  49. }
  50. return true;
  51. }
  52. }