1: <?php
2:
3: class FileSystem {
4:
5: public static function createTemporaryFile($prefix = '') {
6: return tempnam(sys_get_temp_dir(), $prefix);
7: }
8:
9: public static function createTemporaryDirectory($prefix = '') {
10:
11: $dir = tempnam(sys_get_temp_dir(), $prefix);
12: if (file_exists($dir)) {
13: unlink($dir);
14: }
15: mkdir($dir);
16: return $dir;
17: }
18:
19: public static function listFiles($directory, $recursive = false) {
20: return self::_listFiles($directory, '', $recursive);
21: }
22:
23: private static function _listFiles($directory, $prefix, $recursive) {
24: $tree = array();
25: $it = new DirectoryIterator($directory);
26:
27: while ($it->valid()) {
28: if (!$it->isDot()) {
29: $tree[] = $prefix . $it->getFilename();
30: if ($it->isDir() && $recursive) {
31: foreach (self::_listFiles($it->getPathname(), $prefix . $it->getFilename() . '/') as $file) {
32: $tree[] = $file;
33: }
34: }
35: }
36: $it->next();
37: }
38:
39: return $tree;
40: }
41:
42: /**
43: * // ------------ lixlpixel recursive PHP functions -------------
44: // recursive_remove_directory( directory to delete, empty )
45: // expects path to directory and optional TRUE / FALSE to empty
46: // of course PHP has to have the rights to delete the directory
47: // you specify and all files and folders inside the directory
48: // ------------------------------------------------------------
49: // to use this function to totally remove a directory, write:
50: // recursive_remove_directory('path/to/directory/to/delete');
51: // to use this function to empty a directory, write:
52: // recursive_remove_directory('path/to/full_directory',TRUE);
53: * http://lixlpixel.org/recursive_function/php/recursive_directory_delete/
54: */
55: public static function recursiveRemoveDirectory($directory, $empty = FALSE) {
56: // if the path has a slash at the end we remove it here
57: if (substr($directory, -1) == '/') {
58: $directory = substr($directory, 0, -1);
59: }
60:
61: // if the path is not valid or is not a directory ...
62: if (!file_exists($directory) || !is_dir($directory)) {
63: // ... we return false and exit the function
64: return FALSE;
65:
66: // ... if the path is not readable
67: } elseif (!is_readable($directory)) {
68: // ... we return false and exit the function
69: return FALSE;
70:
71: // ... else if the path is readable
72: } else {
73:
74: // we open the directory
75: $handle = opendir($directory);
76:
77: // and scan through the items inside
78: while (FALSE !== ($item = readdir($handle))) {
79: // if the filepointer is not the current directory
80: // or the parent directory
81: if ($item != '.' && $item != '..') {
82: // we build the new path to delete
83: $path = $directory . '/' . $item;
84:
85: // if the new path is a directory
86: if (is_dir($path)) {
87: // we call this function with the new path
88: self::recursiveRemoveDirectory($path);
89:
90: // if the new path is a file
91: } else {
92: // we remove the file
93: unlink($path);
94: }
95: }
96: }
97: // close the directory
98: closedir($handle);
99:
100: // if the option to empty is not set to true
101: if ($empty == FALSE) {
102: // try to delete the now empty directory
103: if (!rmdir($directory)) {
104: // return false if not possible
105: return FALSE;
106: }
107: }
108: // return success
109: return TRUE;
110: }
111: }
112:
113: public static function recursiveRemoveDirectoryInners($directory) {
114: $it = new DirectoryIterator($directory);
115:
116: while ($it->valid()) {
117: if (!$it->isDot()) {
118: if ($it->isFile() && !unlink($it->getPathname())) {
119: return false;
120: } else if (!self::recursiveRemoveDirectory($it->getPathname())) {
121: return false;
122: }
123: }
124: $it->next();
125: }
126:
127: return true;
128: }
129:
130: }