29 const HASH_DIR_FILE_COUNT = 10000;
36 const FILE_SIZE_BYTE =
'B';
37 const FILE_SIZE_KILOBYTE =
'kB';
38 const FILE_SIZE_MEGABYTE =
'MB';
39 const FILE_SIZE_GIGABYTE =
'GB';
60 public static function clearDirectory($path, $deleteRoot=FALSE, array $omitFiles=array())
62 if (file_exists($path) === FALSE) {
66 $dir =
new DirectoryIterator($path);
67 while ($dir->valid() === TRUE) {
68 if ($dir->isDot() === TRUE) {
74 if ($dir->isDir() === TRUE) {
78 if ($deleteRoot === FALSE) {
79 if ($dir->getFileName() ===
'.svn') {
86 if (self::clearDirectory($dir->getPathName(), FALSE) === FALSE) {
91 if (rmdir($dir->getPathName()) === FALSE) {
94 $error =
'Unable to delete directory: '.$dir->getPathName;
95 throw new RuntimeException($error);
103 if (in_array($dir->getFileName(), $omitFiles) === FALSE) {
104 if (unlink($dir->getPathName()) === FALSE) {
106 $error =
'Unable to delete file: '.$dir->getPathName;
107 throw new RuntimeException($error);
114 if ($deleteRoot === TRUE) {
115 if (file_exists($path) === TRUE) {
116 if (rmdir($path) === FALSE) {
117 $error =
'Unable to delete directory: '.$dir->getPathName;
118 throw new RuntimeException($error);
144 public static function listDirectory($path, array $extensions=array(), $nested=TRUE, $fullPath=TRUE)
147 if (file_exists($path) === FALSE) {
151 $dir =
new DirectoryIterator($path);
152 while ($dir->valid() === TRUE) {
153 if ($dir->isDot() === TRUE) {
159 if ($dir->isDir() === TRUE && $nested === TRUE) {
160 if ($dir->getFileName() ===
'.svn') {
166 $nestedDirectory = self::listDirectory($dir->getPathName(), $extensions, $nested, $fullPath);
168 $files = array_merge($files, $nestedDirectory);
175 if ($dir->isFile() === TRUE) {
178 $filename = $dir->getFilename();
181 if ($filename{0} !==
'.') {
183 if (empty($extensions) === TRUE) {
188 $fileName = $dir->getFileName();
189 $dotPos = strrpos($fileName,
'.');
190 $fileExt = substr($fileName, $dotPos);
191 if (in_array($fileExt, $extensions) === TRUE) {
197 if ($addFile === TRUE) {
200 if ($fullPath === TRUE) {
201 $files[] = $dir->getPathName();
203 $files[] = $dir->getFileName();
232 $dir =
new RecursiveDirectoryIterator($path);
235 foreach ($dir as $file) {
236 if ($file->isDir() === TRUE) {
237 if ($file->getFilename() === $name) {
238 $paths[] = $file->getPathname();
240 $paths = array_merge($paths, self::findDirectories($file->getPathName(), $name));
263 throw new InvalidArgumentException(
'$id must be positive');
266 $endId = (ceil(($id / self::HASH_DIR_FILE_COUNT)) * self::HASH_DIR_FILE_COUNT);
267 $startId = ($endId - self::HASH_DIR_FILE_COUNT + 1);
269 return $startId.
'-'.$endId;
285 $size = @filesize($path);
286 if ($size === FALSE) {
291 self::FILE_SIZE_BYTE,
292 self::FILE_SIZE_KILOBYTE,
293 self::FILE_SIZE_MEGABYTE,
294 self::FILE_SIZE_GIGABYTE,
296 $maxUnit = (count($units) - 1);
299 $unit = array_search($unit, $units);
300 if ($unit === NULL || $unit === FALSE) {
305 while ($unit !== $factor && $size >= 1000 && $factor < $maxUnit) {
306 $size = ($size / 1000);
310 return sprintf(
'%01.2f%s', $size, $units[$factor]);
325 $fileName = preg_replace(
'/^.+[\\\\\\/]/',
'', $path);