Source for file Util.php
Documentation is available at Util.php
// +----------------------------------------------------------------------+
// | PEAR :: File :: Util |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is available at http://www.php.net/license/3_0.txt |
// | If you did not receive a copy of the PHP license and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Copyright (c) 2005 Michael Wallner <mike@iworks.at> |
// +----------------------------------------------------------------------+
// $Id: Util.php,v 1.6 2005/01/12 08:46:00 mike Exp $
define('FILE_SORT_REVERSE', 1 );
define('FILE_LIST_ALL', FILE_LIST_FILES | FILE_LIST_DIRS | FILE_LIST_DOTS );
* File and directory utility functions.
* Returns a string path built from the array $pathParts. Where a join
* occurs multiple separators are removed. Joins using the optional
* separator, defaulting to the PHP DIRECTORY_SEPARATOR constant.
* @param array $parts Array containing the parts to be joined
* @param string $separator The directory seperator
function buildPath ($parts, $separator = DIRECTORY_SEPARATOR )
* @FIXXME: maybe better use foreach
for ($i = 0 , $c = count($parts); $i < $c; $i++ ) {
$parts[$i] = rtrim($parts[$i], $separator);
} elseif ($c - 1 == $i) {
$parts[$i] = ltrim($parts[$i], $separator);
$parts[$i] = trim($parts[$i], $separator);
return implode($separator, $parts);
* Returns a path without leading / or C:\. If this is not
* present the path is returned as is.
* @param string $path The path to be processed
* @return string The processed path or the path as is
if (File_Util ::isAbsolute ($path)) {
return substr($path, $path{3 } == '\\' ? 4 : 3 );
return ltrim($path, '/');
* Returns the temp directory according to either the TMP, TMPDIR, or
* TEMP env variables. If these are not set it will also check for the
* existence of /tmp, %WINDIR%\temp
* @return string The system tmp directory
if (isset ($_ENV['TEMP'])) {
if (isset ($_ENV['TMP'])) {
if (isset ($_ENV['windir'])) {
return $_ENV['windir'] . '\\temp';
if (isset ($_ENV['SystemRoot'])) {
return $_ENV['SystemRoot'] . '\\temp';
if (isset ($_SERVER['TEMP'])) {
if (isset ($_SERVER['TMP'])) {
if (isset ($_SERVER['windir'])) {
return $_SERVER['windir'] . '\\temp';
if (isset ($_SERVER['SystemRoot'])) {
return $_SERVER['SystemRoot'] . '\\temp';
if (isset ($_ENV['TMPDIR'])) {
if (isset ($_SERVER['TMPDIR'])) {
return $_SERVER['TMPDIR'];
* Returns a temporary filename using tempnam() and File::tmpDir().
* @param string $dirname Optional directory name for the tmp file
* @return string Filename and path of the tmp file
function tmpFile ($dirname = null )
$dirname = File_Util ::tmpDir ();
* Returns boolean based on whether given path is absolute or not.
* @param string $path Given path
* @return boolean True if the path is absolute, false if it is not
function isAbsolute ($path)
return ($path{0 } == '/') || ($path{0 } == '~');
* Get path relative to another path
* @param string $separator
function relativePath ($path, $root, $separator = DIRECTORY_SEPARATOR )
$path = File_Util ::realpath ($path, $separator);
$root = File_Util ::realpath ($root, $separator);
$dirs = explode($separator, $path);
$comp = explode($separator, $root);
unset ($dirs[0 ], $comp[0 ]);
foreach ($comp as $i => $part) {
if (isset ($dirs[$i]) && $part == $dirs[$i]) {
unset ($dirs[$i], $comp[$i]);
* Get real path (works with non-existant paths)
* @param string $separator
function realPath ($path, $separator = DIRECTORY_SEPARATOR )
if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
if ($path{0 } !== $separator{0 }) {
$path = substr($cwd, 3 ) . $separator . $path;
} elseif ($path{0 } !== $separator) {
$path = $cwd . $separator .
File_Util ::relativePath ($path, $cwd, $separator);
foreach (explode($separator, $path) as $dir) {
if (strlen($dir) && $dir !== '.') {
return $drive . $separator . implode($separator, $dirStack);
* Check whether path is in root path
function pathInRoot ($path, $root)
static $realPaths = array ();
if (!isset ($realPaths[$root])) {
$realPaths[$root] = File_Util ::realPath ($root);
return false !== strstr (File_Util ::realPath ($path), $realPaths[$root]);
function listDir ($path, $list = FILE_LIST_ALL , $sort = FILE_SORT_NONE )
for ($dir = dir($path); false !== $entry = $dir->read (); ) {
$isRef = ($entry === '.' || $entry === '..');
$isDir = $isRef || is_dir($path . '/'. $entry);
$entries[] = (object) array (
'size' => $isDir ? null : filesize($path . '/'. $entry),
$entries = File_Util ::sortFiles ($entries, $sort);
function sortFiles ($files, $sort)
foreach ($files as $file) {
$names[] = $file['name'];
$sizes[] = $file['size'];
$dates[] = $file['date'];
arsort($r, $sortFlags[$sort & ~1 ]);
asort($r, $sortFlags[$sort]);
foreach ($r as $i => $f) {
Documentation generated on Mon, 11 Mar 2019 14:12:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|