4 * Utility class for static directory methods.
10 * @author Paul M. Jones <pmjones@solarphp.com>
12 * @license http://opensource.org/licenses/bsd-license.php BSD
14 * @version $Id: Dir.php 2926 2007-11-09 16:25:44Z pmjones $
21 * The OS-specific temporary directory location.
26 protected static $_tmp;
30 * Hack for [[php::is_dir() | ]] that checks the include_path.
32 * Use this to see if a directory exists anywhere in the include_path.
35 * $dir = Solar_Dir::exists('path/to/dir')
37 * $files = scandir($dir);
39 * echo "Not found in the include-path.";
43 * @param string $dir Check for this directory in the include_path.
45 * @return mixed If the directory exists in the include_path, returns the
46 * absolute path; if not, returns boolean false.
49 public static function exists($dir)
57 // using an absolute path for the file?
58 // dual check for Unix '/' and Windows '\',
59 // or Windows drive letter and a ':'.
60 $abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':');
61 if ($abs && is_dir($dir)) {
65 // using a relative path on the file
66 $path = explode(PATH_SEPARATOR, ini_get('include_path'));
67 foreach ($path as $base) {
68 // strip Unix '/' and Windows '\'
69 $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir;
70 if (is_dir($target)) {
81 * "Fixes" a directory string for the operating system.
83 * Use slashes anywhere you need a directory separator. Then run the
84 * string through fixdir() and the slashes will be converted to the
85 * proper separator (for example '\' on Windows).
87 * Always adds a final trailing separator.
89 * @param string $dir The directory string to 'fix'.
91 * @return string The "fixed" directory string.
94 public static function fix($dir)
96 $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
97 return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
102 * Convenience method for dirname() and higher-level directories.
104 * @param string $file Get the dirname() of this file.
106 * @param int $up Move up in the directory structure this many
109 * @return string The dirname() of the file.
112 public static function name($file, $up = 0)
114 $dir = dirname($file);
116 $dir = dirname($dir);
123 * Returns the OS-specific directory for temporary files.
125 * @param string $sub Add this subdirectory to the returned temporary
128 * @return string The temporary directory path.
131 public static function tmp($sub = '')
133 // find the tmp dir if needed
134 if (! Solar_Dir::$_tmp) {
136 // use the system if we can
137 if (function_exists('common_get_temp_dir')) {
138 $tmp = common_get_temp_dir();
140 $tmp = Solar_Dir::_tmp();
143 // remove trailing separator and save
144 Solar_Dir::$_tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
147 // do we have a subdirectory request?
150 // remove leading and trailing separators, and force exactly
151 // one trailing separator
152 $sub = trim($sub, DIRECTORY_SEPARATOR)
153 . DIRECTORY_SEPARATOR;
156 return Solar_Dir::$_tmp . DIRECTORY_SEPARATOR . $sub;
161 * Returns the OS-specific temporary directory location.
163 * @return string The temp directory path.
166 protected static function _tmp()
168 // non-Windows system?
169 if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
170 $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
179 $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
185 $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
191 $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
196 // final fallback for Windows
197 return getenv('SystemRoot') . '\\temp';