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