3 /** This file is part of KCFinder project
5 * @desc Directory helper class
8 * @author Pavel Tzonkov <sunhater@sunhater.com>
9 * @copyright 2010-2014 KCFinder Project
10 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
11 * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
12 * @link http://kcfinder.sunhater.com
19 /** Checks if the given directory is really writable. The standard PHP
20 * function is_writable() does not work properly on Windows servers
24 static function isWritable($dir) {
25 $dir = path
::normalize($dir);
30 $file = "$dir/is_writable_" . md5($i++
);
31 } while (file_exists($file));
38 /** Recursively delete the given directory. Returns TRUE on success.
39 * If $firstFailExit parameter is true (default), the method returns the
40 * path to the first failed file or directory which cannot be deleted.
41 * If $firstFailExit is false, the method returns an array with failed
42 * files and directories which cannot be deleted. The third parameter
43 * $failed is used for internal use only.
45 * @param bool $firstFailExit
46 * @param array $failed
49 static function prune($dir, $firstFailExit=true, array $failed=null) {
50 if ($failed === null) $failed = array();
51 $files = self
::content($dir);
52 if ($files === false) {
59 foreach ($files as $file) {
61 $failed_in = self
::prune($file, $firstFailExit, $failed);
62 if ($failed_in !== true) {
65 if (is_array($failed_in))
66 $failed = array_merge($failed, $failed_in);
68 $failed[] = $failed_in;
70 } elseif (!@unlink
($file)) {
83 return count($failed) ?
$failed : true;
86 /** Get the content of the given directory. Returns an array with filenames
89 * @param array $options
92 static function content($dir, array $options=null) {
94 $defaultOptions = array(
95 'types' => "all", // Allowed: "all" or possible return values
96 // of filetype(), or an array with them
97 'addPath' => true, // Whether to add directory path to filenames
98 'pattern' => '/./', // Regular expression pattern for filename
102 if (!is_dir($dir) ||
!is_readable($dir))
105 if (strtoupper(substr(PHP_OS
, 0, 3)) == "WIN")
106 $dir = str_replace("\\", "/", $dir);
107 $dir = rtrim($dir, "/");
109 $dh = @opendir
($dir);
113 if ($options === null)
114 $options = $defaultOptions;
116 foreach ($defaultOptions as $key => $val)
117 if (!isset($options[$key]))
118 $options[$key] = $val;
121 while (($file = @readdir
($dh)) !== false) {
123 if (($file == '.') ||
($file == '..') ||
124 !preg_match($options['pattern'], $file)
128 $fullpath = "$dir/$file";
129 $type = filetype($fullpath);
131 // If file is a symlink, get the true type of its destination
132 if ($options['followLinks'] && ($type == "link"))
133 $type = filetype(realpath($fullpath));
135 if (($options['types'] === "all") ||
($type === $options['types']) ||
136 (is_array($options['types']) && in_array($type, $options['types']))
138 $files[] = $options['addPath'] ?
$fullpath : $file;
141 usort($files, array(__NAMESPACE__
. "\\dir", "fileSort"));
145 static function fileSort($a, $b) {
146 if (function_exists("mb_strtolower")) {
147 $a = mb_strtolower($a);
148 $b = mb_strtolower($b);
153 if ($a == $b) return 0;
154 return ($a < $b) ?
-1 : 1;