1: <?php
2:
3: /**
4: * Class QFolder: Handles folders located on your filesystem.
5: */
6: class QFolder {
7: /**
8: * Same as mkdir but correctly implements directory recursion.
9: * At its core, it will use the php MKDIR function.
10: * This method does no special error handling. If you want to use special error handlers,
11: * be sure to set that up BEFORE calling MakeDirectory.
12: *
13: * @param string $strPath actual path of the directoy you want created
14: * @param integer $intMode optional mode
15: *
16: * @return boolean the return flag from mkdir
17: */
18: public static function MakeDirectory($strPath, $intMode = null) {
19: if (is_dir($strPath)) {
20: // Directory Already Exists
21: return true;
22: }
23:
24: // Check to make sure the parent(s) exist, or create if not
25: if (!self::MakeDirectory(dirname($strPath), $intMode)) {
26: return false;
27: }
28:
29: if (PHP_OS != "Linux") {
30: // Create the current node/directory, and return its result
31: $blnReturn = mkdir($strPath);
32:
33: if ($blnReturn && !is_null($intMode)) {
34: // Manually CHMOD to $intMode (if applicable)
35: // mkdir doesn't do it for mac, and this will error on windows
36: // Therefore, ignore any errors that creep up
37: QApplication::SetErrorHandler(null);
38: chmod($strPath, $intMode);
39: QApplication::RestoreErrorHandler();
40: }
41: } else {
42: $blnReturn = mkdir($strPath, $intMode);
43: }
44:
45: return $blnReturn;
46: }
47:
48: /**
49: * Allows for deletion of non-empty directories - takes care of
50: * recursion appropriately.
51: *
52: * @param string $strPath Full path to the folder to be deleted
53: *
54: * @return int number of deleted files
55: */
56: public static function DeleteFolder($strPath) {
57: if (!is_dir($strPath)) {
58: unlink($strPath);
59:
60: return 1;
61: }
62:
63: $d = dir($strPath);
64: $count = 0;
65: while ($entry = $d->read()) {
66: if ($entry != "." && $entry != "..") {
67: if (is_dir($strPath)) {
68: $count += QFolder::DeleteFolder($strPath . "/" . $entry);
69: }
70: }
71: }
72:
73: $d->close();
74: rmdir($strPath);
75:
76: return $count;
77: }
78:
79: /**
80: * Tells whether a folder is writable or not.
81: * Uses the QFile method underneath
82: *
83: * @param string $strPath Path to the folder.
84: *
85: * @return bool
86: */
87: public static function isWritable($strPath) {
88: if ($strPath[strlen($strPath) - 1] != "/") {
89: $strPath .= "/";
90: }
91:
92: return QFile::isWritable($strPath);
93: }
94:
95: /**
96: * Helper function for getRecursiveFolderContents.
97: * Returns the list of files in the folder that match a given pattern
98: * Result is an array of strings of form "filename.extension".
99: * Not recursive!
100: *
101: * @param string $strPath full path to the folder to be processed
102: *
103: * @return array
104: */
105: private static function getFilesInFolderHelper($strPath) {
106: // Remove trailing slash if it's there
107: if ($strPath[strlen($strPath) - 1] == "/") {
108: $strPath = substr($strPath, 0, -1);
109: }
110: $result = array();
111: $dh = opendir($strPath);
112: assert ('$dh !== false'); // Does directory exist?
113: if ($dh === false) return false;
114:
115: while (($file = readdir($dh)) !== false) {
116: if ($file != "." && $file != "..") {
117: if (!is_dir($file)) {
118: array_push($result, $file);
119: }
120: }
121: }
122: closedir($dh);
123:
124: return $result;
125: }
126:
127: /**
128: * Traverse a particular path and get a list of files and folders
129: * underneath that path. Optionally, also provide a regular
130: * expression that specifies the pattern that the returned files must match.
131: *
132: * @param string $strPath full path to the folder to be processed
133: * @param boolean $blnSkipFolders If this is set to true, only the FILES will be returned - not the folders.
134: * @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.
135: *
136: * @return array
137: */
138: public static function listFilesInFolder($strPath, $blnSkipFolders = true, $strFilenamePattern = null) {
139: // strip off the trailing slash if it's there
140: if ($strPath[strlen($strPath) - 1] == "/") {
141: $strPath = substr($strPath, 0, -1);
142: }
143:
144: $result = array();
145:
146: $originalSet = self::getFilesInFolderHelper($strPath);
147: if (!$originalSet) return $result; // empty directory, or directory does not exist
148:
149: foreach ($originalSet as $item) {
150: $childPath = $strPath . "/" . $item;
151: if (is_dir($childPath)) {
152: $childItems = self::listFilesInFolder($childPath);
153: foreach ($childItems as $child) {
154: $result[] = $item . "/" . $child;
155: }
156: }
157:
158: if (!$blnSkipFolders || !is_dir($childPath)) {
159: if (!$strFilenamePattern || ($strFilenamePattern && preg_match($strFilenamePattern, $item))) {
160: $result[] = $item;
161: }
162: }
163: }
164:
165: return $result;
166: }
167: }