-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFolder.php
More file actions
263 lines (234 loc) · 8.03 KB
/
Copy pathFolder.php
File metadata and controls
263 lines (234 loc) · 8.03 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
*
* Part of the QCubed PHP framework.
*
* @license MIT
*
*/
namespace QCubed;
/**
* Class QFolder
* Handles folders(directories) located on your filesystem
* @package QCubed
* @was QFolder
*/
abstract class Folder
{
/**
* Same as mkdir but correctly implements directory recursion.
* At its core, it will use the php MKDIR function.
* This method does no special error handling. If you want to use special error handlers,
* be sure to set that up BEFORE calling MakeDirectory.
*
* @param string $strPath actual path of the directoy you want created
* @param integer $intMode optional mode
*
* @return boolean the return flag from mkdir
*/
public static function makeDirectory($strPath, $intMode = null)
{
if (is_dir($strPath)) {
// Directory Already Exists
return true;
}
// Check to make sure the parent(s) exist, or create if not
if (!self::makeDirectory(dirname($strPath), $intMode)) {
return false;
}
if (PHP_OS != "Linux") {
// Create the current node/directory, and return its result
$blnReturn = mkdir($strPath);
if ($blnReturn && !is_null($intMode)) {
// Manually CHMOD to $intMode (if applicable)
// mkdir doesn't do it for mac, and this will error on windows
// Therefore, ignore any errors that creep up
$e = new Error\Handler();
chmod($strPath, $intMode);
}
} else {
$blnReturn = mkdir($strPath, $intMode);
}
return $blnReturn;
}
/**
* Allows for deletion of non-empty directories - takes care of
* recursion appropriately.
*
* @param string $strPath Full path to the folder to be deleted
*
* @return int number of deleted files
*/
public static function deleteFolder($strPath)
{
if (!is_dir($strPath)) {
unlink($strPath);
return 1;
}
$d = dir($strPath);
$count = 0;
while ($entry = $d->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($strPath)) {
$count += Folder::deleteFolder($strPath . "/" . $entry);
}
}
}
$d->close();
rmdir($strPath);
return $count;
}
/**
* Tells whether a folder is writable or not.
* Uses the QFile method underneath
*
* @param string $strPath Path to the folder.
*
* @return bool
*/
public static function isWritable($strPath)
{
if ($strPath[strlen($strPath) - 1] != "/") {
$strPath .= "/";
}
return File::isWritable($strPath);
}
/**
* Traverse a particular path and get a list of files and folders
* underneath that path. Optionally, also provide a regular
* expression that specifies the pattern that the returned files must match.
*
* @param string $strPath full path to the folder to be processed
* @param boolean $blnSkipFolders If this is set to true, only the FILES will be returned - not the folders.
* @param string $strFilenamePattern : optional string; regular expression that the files must match in order to be returned. If it's not set, all files in that folder will be returned.
*
* @return array
*/
public static function listFilesInFolder($strPath, $blnSkipFolders = true, $strFilenamePattern = null)
{
// strip off the trailing slash if it's there
if ($strPath[strlen($strPath) - 1] == "/") {
$strPath = substr($strPath, 0, -1);
}
$result = array();
$originalSet = self::getFilesInFolderHelper($strPath);
if (!$originalSet) {
return $result;
} // empty directory, or directory does not exist
foreach ($originalSet as $item) {
$childPath = $strPath . "/" . $item;
if (is_dir($childPath)) {
$childItems = self::listFilesInFolder($childPath);
foreach ($childItems as $child) {
$result[] = $item . "/" . $child;
}
}
if (!$blnSkipFolders || !is_dir($childPath)) {
if (!$strFilenamePattern || ($strFilenamePattern && preg_match($strFilenamePattern, $item))) {
$result[] = $item;
}
}
}
return $result;
}
/**
* Helper function for getRecursiveFolderContents.
* Returns the list of files in the folder that match a given pattern
* Result is an array of strings of form "filename.extension".
* Not recursive!
*
*
* @param string $strPath full path to the folder to be processed
*
* @return array
*/
private static function getFilesInFolderHelper($strPath)
{
// Remove trailing slash if it's there
if ($strPath[strlen($strPath) - 1] == "/") {
$strPath = substr($strPath, 0, -1);
}
$result = array();
$dh = opendir($strPath);
assert($dh !== false); // Does directory exist?
if ($dh === false) {
return [];
} // if asserts are off
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
if (!is_dir($file)) {
array_push($result, $file);
}
}
}
closedir($dh);
return $result;
}
/**
* Copy the contents of the source directory into the destination directory, creating the destination directory
* and subdirectories if they do not exist. You can control whether to overwrite existing files or not.
*
* @param string $srcPath source directory
* @param string $dstPath destination directory
* @param boolean $blnOverwrite True to overwrite a file if it already exists. False to leave existing files alone.
*/
public static function mergFolders($srcPath, $dstPath, $blnOverwrite)
{
if (!$srcPath || !is_dir($srcPath)) {
return;
}
$dir = opendir($srcPath);
try {
if (!file_exists($dstPath)) {
mkdir($dstPath);
}
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($srcPath . '/' . $file)) {
self::copy_dir($srcPath . '/' . $file, $dstPath . '/' . $file, $blnOverwrite);
} else {
if ($blnOverwrite) {
copy($srcPath . '/' . $file, $dstPath . '/' . $file);
} else {
if (!file_exists($dstPath . '/' . $file)) {
copy($srcPath . '/' . $file, $dstPath . '/' . $file);
}
}
}
}
}
}
finally {
closedir($dir);
}
}
/**
* Returns the number of items in a directory, whether they are files or other directories. Does not count the
* dot and dot-dot items.
* @param string $dir
* @return int
*/
public static function countItems($dir) {
$fi = new \FilesystemIterator($dir, \FilesystemIterator::SKIP_DOTS);
return iterator_count($fi);
}
/**
* Removes all the items in the given directory, while preserving the directory itself.
*
* @param string $dir
*/
public static function emptyContents($dir) {
$iterator = new \FilesystemIterator($dir, \FilesystemIterator::SKIP_DOTS);
if (!$iterator->valid()) {
return;
}
foreach($iterator as $fileinfo) {
if ($fileinfo->isDir()) {
self::deleteFolder($fileinfo->getPathname());
}
else {
unlink($fileinfo->getPathname());
}
}
}
}