folder.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 if (!class_exists('Object')) {
00032 uses ('object');
00033 }
00034
00035
00036
00037
00038
00039
00040
00041
00042 class Folder extends Object{
00043
00044
00045
00046
00047
00048 var $path = null;
00049
00050
00051
00052
00053
00054 var $sort = false;
00055
00056
00057
00058
00059
00060
00061 function __construct($path = false, $create = false, $mode = false) {
00062 parent::__construct();
00063 if (empty($path)) {
00064 $path = getcwd();
00065 }
00066
00067 if (!file_exists($path) && $create == true) {
00068 $this->mkdirr($path, $mode);
00069 }
00070 $this->cd($path);
00071 }
00072
00073
00074
00075
00076
00077 function pwd() {
00078 return $this->path;
00079 }
00080
00081
00082
00083
00084
00085
00086 function cd($desiredPath) {
00087 $desiredPath = realpath($desiredPath);
00088 $newPath = $this->isAbsolute($desiredPath) ? $desiredPath : $this->addPathElement($this->path, $desiredPath);
00089 $isDir = (is_dir($newPath) && file_exists($newPath)) ? $this->path = $newPath : false;
00090 return $isDir;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100 function ls($sort = true, $noDotFiles = false) {
00101 $dirs = $files = array();
00102 $dir = opendir($this->path);
00103 if ($dir) {
00104 while (false !== ($n = readdir($dir))) {
00105 if ((!preg_match('#^\.+$#', $n) && $noDotFiles == false) || ($noDotFiles == true && !preg_match('#^\.(.*)$#', $n))) {
00106 if (is_dir($this->addPathElement($this->path, $n))) {
00107 $dirs[] = $n;
00108 } else {
00109 $files[] = $n;
00110 }
00111 }
00112 }
00113
00114 if ($sort || $this->sort) {
00115 sort ($dirs);
00116 sort ($files);
00117 }
00118 closedir ($dir);
00119 }
00120 return array($dirs,$files);
00121 }
00122
00123
00124
00125
00126
00127
00128 function find($regexp_pattern = '.*') {
00129 $data = $this->ls();
00130
00131 if (!is_array($data)) {
00132 return array();
00133 }
00134
00135 list($dirs, $files) = $data;
00136 $found = array();
00137
00138 foreach ($files as $file) {
00139 if (preg_match("/^{$regexp_pattern}$/i", $file)) {
00140 $found[] = $file;
00141 }
00142 }
00143 return $found;
00144 }
00145
00146
00147
00148
00149
00150
00151 function findRecursive($pattern = '.*') {
00152 $startsOn = $this->path;
00153 $out = $this->_findRecursive($pattern);
00154 $this->cd($startsOn);
00155 return $out;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 function _findRecursive($pattern) {
00165 list($dirs, $files) = $this->ls();
00166
00167 $found = array();
00168 foreach ($files as $file) {
00169 if (preg_match("/^{$pattern}$/i", $file)) {
00170 $found[] = $this->addPathElement($this->path, $file);
00171 }
00172 }
00173 $start = $this->path;
00174 foreach ($dirs as $dir) {
00175 $this->cd($this->addPathElement($start, $dir));
00176 $found = array_merge($found, $this->findRecursive($pattern));
00177 }
00178 return $found;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187 function isWindowsPath($path) {
00188 $match = preg_match('#^[A-Z]:\\\#i', $path) ? true : false;
00189 return $match;
00190 }
00191
00192
00193
00194
00195
00196
00197
00198 function isAbsolute($path) {
00199 $match = preg_match('#^\/#', $path) || preg_match('#^[A-Z]:\\\#i', $path);
00200 return $match;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 function isSlashTerm($path) {
00210 $match = preg_match('#[\\\/]$#', $path) ? true : false;
00211 return $match;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220 function correctSlashFor($path) {
00221 return $this->isWindowsPath($path) ? '\\' : '/';
00222 }
00223
00224
00225
00226
00227
00228
00229
00230 function slashTerm($path) {
00231 return $path . ($this->isSlashTerm($path) ? null : $this->correctSlashFor($path));
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241 function addPathElement($path, $element) {
00242 return $this->slashTerm($path) . $element;
00243 }
00244
00245
00246
00247
00248
00249 function inCakePath($path = '') {
00250 $dir = substr($this->slashTerm(ROOT), 0, -1);
00251 $newdir = $this->slashTerm($dir . $path);
00252 return $this->inPath($newdir);
00253 }
00254
00255
00256
00257
00258
00259 function inPath($path = '') {
00260 $dir = substr($this->slashTerm($path), 0, -1);
00261 $return = preg_match('/^' . preg_quote($this->slashTerm($dir), '/') . '(.*)/', $this->slashTerm($this->pwd()));
00262 if ($return == 1) {
00263 return true;
00264 } else {
00265 return false;
00266 }
00267 }
00268
00269
00270
00271
00272
00273
00274 function mkdirr($pathname, $mode = null) {
00275 if (is_dir($pathname) || empty($pathname)) {
00276 return true;
00277 }
00278
00279 if (is_file($pathname)) {
00280 trigger_error('mkdirr() File exists', E_USER_WARNING);
00281 return false;
00282 }
00283 $nextPathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR));
00284
00285 if ($this->mkdirr($nextPathname, $mode)) {
00286 if (!file_exists($pathname)) {
00287 umask (0);
00288 $mkdir = mkdir($pathname, $mode);
00289 return $mkdir;
00290 }
00291 }
00292 return false;
00293 }
00294
00295
00296
00297
00298
00299 function dirsize() {
00300 $size = 0;
00301 $directory = $this->slashTerm($this->path);
00302 $stack = array($directory);
00303 $count = count($stack);
00304 for ($i = 0, $j = $count; $i < $j; ++$i) {
00305 if (is_file($stack[$i])) {
00306 $size += filesize($stack[$i]);
00307 } elseif (is_dir($stack[$i])) {
00308 $dir = dir($stack[$i]);
00309
00310 while (false !== ($entry = $dir->read())) {
00311 if ($entry == '.' || $entry == '..') {
00312 continue;
00313 }
00314 $add = $stack[$i] . $entry;
00315
00316 if (is_dir($stack[$i] . $entry)) {
00317 $add = $this->slashTerm($add);
00318 }
00319 $stack[ ]= $add;
00320 }
00321 $dir->close();
00322 }
00323 $j = count($stack);
00324 }
00325 return $size;
00326 }
00327 }
00328 ?>