1
+ <?php
2
+ require_once (__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator ' . DIRECTORY_SEPARATOR . 'filter-dots.php ' );
3
+ require_once (__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator ' . DIRECTORY_SEPARATOR . 'filter-extension.php ' );
4
+ require_once (__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator ' . DIRECTORY_SEPARATOR . 'filter-key.php ' );
5
+ require_once (__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator ' . DIRECTORY_SEPARATOR . 'directory-tree.php ' );
6
+ require_once (__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator ' . DIRECTORY_SEPARATOR . 'directory-graph.php ' );
7
+ require_once (__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator ' . DIRECTORY_SEPARATOR . 'directory-match.php ' );
8
+
9
+ /**
10
+ * This class contains example usage for the entire directory-iterator sub
11
+ * directory.
12
+ *
13
+ * http://www.phpclasses.org/package/4389-PHP-Retrieve-directory-listings-with-SPL-iterators.html
14
+ *
15
+ * @author Paul Scott <pscott@uwc.ac.za>
16
+ */
17
+ class DirectoryUsage
18
+ {
19
+ /**
20
+ * Recursively list the contents of a directory. Second parameter allows you
21
+ * to specify whether you'd like to only return a list of directories or files.
22
+ *
23
+ * @param string $dir
24
+ * @param string $type ['file'|'dir']
25
+ * @return array
26
+ */
27
+ public function dirListByType ($ dir , $ type = 'file ' )
28
+ {
29
+ $ output = array ();
30
+
31
+ $ it = new RecursiveDirectoryIterator ($ dir );
32
+ $ it = new RecursiveIteratorIterator ($ it , RecursiveIteratorIterator::CHILD_FIRST );
33
+ foreach ($ it as $ file ) {
34
+ if ($ file ->getType () == $ type ) {
35
+ $ output [] = $ file ->getPathname ();
36
+ }
37
+ }
38
+
39
+ return $ output ;
40
+ }
41
+
42
+ /**
43
+ * Example of using SPL to clean up directories by removing specific filenames.
44
+ *
45
+ * @access public
46
+ * @param string $directory
47
+ * @param array $filter
48
+ */
49
+ public function cleanDir ($ directory , $ filter = array ('_vti_cnf ' , '_vti_private ' , '_vti_txt ' , '_private ' , '_themes ' , 'msupdate ' , 'vti_pvt ' , 'vti_script ' , '_vti_log ' , '_template ' ,'Thumbs.db ' ))
50
+ {
51
+ $ it = new RecursiveDirectoryIterator ($ directory );
52
+ $ it = new RecursiveIteratorIterator ($ it , RecursiveIteratorIterator::CHILD_FIRST );
53
+ foreach ($ it as $ file ) {
54
+
55
+ // remove empty dirs
56
+ if (sizeof ($ file ->getSize ()) == 0 ) {
57
+ unlink ($ file ->getPath ());
58
+ }
59
+
60
+ // remove instances of Thumbs.db
61
+ if ($ file ->getFileName () == 'Thumbs.db ' ) {
62
+ unlink ($ file ->getPath () . DIRECTORY_SEPARATOR . $ file ->getFilename ());
63
+ }
64
+
65
+ // if paths match filter, delete directory recursively
66
+ $ parts = explode (DIRECTORY_SEPARATOR , $ file ->getPath ());
67
+ if (in_array (end ($ parts ), $ filter )) {
68
+ $ this ->deleteDir ($ file ->getPath ());
69
+ }
70
+
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Method to get information about all the files in a directory (recursive)
76
+ *
77
+ * @param string $directory
78
+ * @param array $filter
79
+ * @return array
80
+ */
81
+ public function fileInfo ($ directory , $ filter = array ('php ' , 'xsl ' , 'xml ' , 'htm ' , 'html ' ,'css ' ))
82
+ {
83
+ $ count_directories = 0 ;
84
+ $ count_files = 0 ;
85
+ $ count_lines = 0 ;
86
+ $ count_bytes = 0 ;
87
+
88
+ $ it = new RecursiveDirectoryIterator ($ directory );
89
+ $ it = new RecursiveIteratorIterator ($ iterator , RecursiveIteratorIterator::CHILD_FIRST );
90
+
91
+ foreach ($ it as $ file ) {
92
+ if (false === $ file ->isDir ()) {
93
+ // get the file extension
94
+ $ ext = $ file ->getExtension ();
95
+ if (in_array ($ ext , $ filter )) {
96
+ $ count_files ++;
97
+ $ count_bytes += $ file ->getSize ();
98
+ $ count_lines += sizeof (explode ("n " , file_get_contents ($ file ->getPathName ())));
99
+ }
100
+ } else if (false === strpos ($ file ->getPathname (), 'CVS ' ) && $ file ->isDir ()) {
101
+ $ count_directories ++;
102
+ }
103
+ }
104
+
105
+ return array (
106
+ 'bytes ' => $ count_bytes ,
107
+ 'files ' => $ count_files ,
108
+ 'lines ' => $ count_lines ,
109
+ 'directories ' => $ count_directories
110
+ );
111
+ }
112
+
113
+ /**
114
+ * Recursively delete a directory and all subdirectories.
115
+ *
116
+ * @access public
117
+ * @param string $dir
118
+ * @return void
119
+ */
120
+ public function deleteDir ($ dir )
121
+ {
122
+ $ it = new RecursiveDirectoryIterator ($ dir );
123
+ $ it = new RecursiveIteratorIterator ($ iterator , RecursiveIteratorIterator::CHILD_FIRST );
124
+ foreach ($ it as $ file ) {
125
+ if ($ file ->isDir ()) {
126
+ rmdir ($ file ->getPathname ());
127
+ } else {
128
+ unlink ($ file ->getPathname ());
129
+ @rmdir ($ dir );
130
+ }
131
+ }
132
+ @rmdir ($ dir );
133
+ }
134
+
135
+ /**
136
+ * Find a file by regex in a given directory.
137
+ *
138
+ * @param string $path
139
+ * @param string $regex
140
+ * @return array
141
+ */
142
+ public function fileFinder ($ path , $ regex )
143
+ {
144
+ $ matches = array ();
145
+
146
+ $ fileList = new DirMatch ($ path , $ regex );
147
+ foreach ($ fileList as $ file ) {
148
+ $ matches [] = $ file ;
149
+ }
150
+
151
+ return $ matches ;
152
+ }
153
+
154
+ /**
155
+ * List files in a given directory.
156
+ *
157
+ * @param string $dir
158
+ * @return array
159
+ */
160
+ public function fileLister ($ dir )
161
+ {
162
+ $ files = array ();
163
+
164
+ $ filtered = new DirectoryFilterDots ($ dir );
165
+ foreach ($ filtered as $ file ) {
166
+ if ($ file ->isDir ()) {
167
+ continue ;
168
+ }
169
+ $ files [] = $ file ->getFilename ();
170
+ }
171
+
172
+ return $ files ;
173
+ }
174
+
175
+ }
176
+
177
+ // generate the directory path to the example dir
178
+ $ dir = __DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator-example ' ;
179
+
180
+ // load up the class
181
+ $ DirectoryUsage = new DirectoryUsage ();
182
+
183
+ echo '================================== ' . PHP_EOL ;
184
+ echo 'Recursively show all files in a directory. ' . PHP_EOL ;
185
+ echo '================================== ' . PHP_EOL ;
186
+
187
+ $ files = $ DirectoryUsage ->dirListByType ($ dir , 'file ' );
188
+ foreach ($ files as $ f ) {
189
+ echo $ f . PHP_EOL ;
190
+ }
191
+
192
+ echo '================================== ' . PHP_EOL ;
193
+ echo 'Recursively show all directories in a directory. ' . PHP_EOL ;
194
+ echo '================================== ' . PHP_EOL ;
195
+
196
+ $ dirs = $ DirectoryUsage ->dirListByType ($ dir , 'dir ' );
197
+ foreach ($ dirs as $ d ) {
198
+ echo $ d . PHP_EOL ;
199
+ }
200
+
201
+ echo '================================== ' . PHP_EOL ;
202
+ echo 'Recursively iterate over all files in a directory. ' . PHP_EOL ;
203
+ echo '================================== ' . PHP_EOL ;
204
+
205
+ // recursively generate a tree representation
206
+ $ files = new DirectoryTreeIterator ($ dir );
207
+ foreach ($ files as $ f ) {
208
+ echo $ f . PHP_EOL ;
209
+ }
210
+
211
+ echo '================================== ' . PHP_EOL ;
212
+ echo 'Iterate over a all files in a directory, filtering out dots. ' . PHP_EOL ;
213
+ echo '================================== ' . PHP_EOL ;
214
+
215
+ // recursively generate a tree representation
216
+ $ files = new DirectoryFilterDots ($ dir );
217
+ foreach ($ files as $ f ) {
218
+ echo $ f . PHP_EOL ;
219
+ }
220
+
221
+ echo '================================== ' . PHP_EOL ;
222
+ echo 'Find all files with a PHP extension. ' . PHP_EOL ;
223
+ echo '================================== ' . PHP_EOL ;
224
+
225
+ // filter by PHP file extension
226
+ $ phpFiles = new ExtensionFilter (new DirectoryIterator ($ dir ), 'php ' , $ whitelist =true );
227
+ foreach ($ phpFiles as $ f ) {
228
+ echo $ f ->getPathName () . PHP_EOL ;
229
+ }
230
+
231
+ echo '================================== ' . PHP_EOL ;
232
+ echo 'Find all files without a PHP extension. ' . PHP_EOL ;
233
+ echo '================================== ' . PHP_EOL ;
234
+
235
+ // filter by PHP file extension
236
+ $ phpFiles = new ExtensionFilter (new DirectoryIterator ($ dir ), 'php ' , $ whitelist =false );
237
+ foreach ($ phpFiles as $ f ) {
238
+ echo $ f ->getPathName () . PHP_EOL ;
239
+ }
0 commit comments