Source for file Folders.php
Documentation is available at Folders.php
* Provides the locations of several system and user directories
* independent of the operating system used.
* require_once 'System/Folders.php';
* $sf = new System_Folders();
* If you want the folders to be cached (not re-calculated on every
* read), use System_Folders_Cached.
* @package System_Folders
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/System_Folders
require_once 'OS/Guess.php';
define('SYS_WINDOWS', 'windows');
* Provides the locations of several system and user directories
* independent of the operating system used.
* If a path does not exist or can't be found (error), NULL is returned.
* The class uses both $_ENV and $_SERVER to retrieve the environment
* paths, as this seems to be different between php4 and 5.
* @package System_Folders
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/System_Folders
* The operating system on which
* Gotten from OS_Guess::getSysname()
* Values (are lowercase):
* Use the SYS_* constants to check it
* Known names for the application directory
'Application data', //english
'Anwendungsdaten', //german
'Toepassingsgegevens', //dutch
'Datos de programa', //spanish
'Dados de aplicativos', //portugese
'Programdata', //norwegian
'Henkilokohtainen', //finnish
'Donnees d\'applications',//french
'Dati applicazioni', //italian
'Dane aplikacji', //polish
* Known names for the my documents directory
'My Documents', //english
'Eigene Dateien', //german
* Known names for the my Desktop directory.
'Desktop' //english, german, italian
* Known names for the my documents directory
* Known paths for the documents and settings directory
'C:\\Documents and Settings\\', //english, italian
'C:\\Dokumente und Einstellungen\\' //german
* Known paths for the programs directory on windows.
'C:\\Programme\\', //german
'C:\\Programmi\\', //italian
* Known names for the shared documents directory
* Although the explorer shows "Shared documents"
* or "Gemeinsame Dokumente", the *real* directory
* is only one of the ones here.
* Known paths for the windows directory.
* COM object used in Windows to get
* special folder locations via SpecialFolders()
* @link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/14761fa3-19be-4742-9f91-23b48cd9228f.asp
* If this variable is NULL, it hasn't been created yet.
* If it is FALSE, it cannot be created and used (e.g.
* because COM is not available)
* Constructor; initializes the system variable.
$this->sys = $og->getSysname ();
}//function System_Folders()
* Adds a trailing slash to the given path if there is none.
* Uses DIRECTORY_SEPARATOR, so it works with windows and *nix.
* @param string $strPath The path
* @return string The path with a trailing slash
if (substr($strPath, -1 ) !== DIRECTORY_SEPARATOR ) {
$strPath .= DIRECTORY_SEPARATOR;
}//function addTrailingSlash($strPath)
* Directories in windows environment variables sometimes
* have a double backslash, and this needs to be fixed.
* @param string $strPath The path
* @return string The fixed path
}//function fixWindowsPath($strPath)
* Loops through a list of given paths and checks
* which of them are correct.
* @param array $arPaths Array with paths to test
* @param string $strBase Base directory that shall be prepended to all paths
* @param string $strSuffix String appended to the directory path
* @return string The directory that exists. NULL if none of them matched.
function tryPaths($arPaths, $strBase = '', $strSuffix = '')
foreach ($arPaths as $strName) {
$strTmp = $strBase . $strName . $strSuffix;
}//function tryPaths($arPaths, $strBase = '', $strSuffix = '')
* Loads the COM object into the $objCom variable.
* @return boolean True if it could be loaded, false if not
return $this->objCom !== false;
$this->objCom = new COM ('WScript.Shell');
* Loads a windows path via COM using $objCom.
* @param string $strType See $objCom for allowed values.
* @return mixed False if no path could be obtained, string otherwise
$strPath = $this->objCom->SpecialFolders ($strType);
if (!$strPath || $strPath == '') {
}//function getCOMPath($strType)
* Returns the All Users directory.
* Works on windows only, returns NULL if not found.
* @return string The all users directory
$arEnv = $_SERVER + $_ENV;
if (isset ($arEnv['ALLUSERSPROFILE'])
&& is_dir($arEnv['ALLUSERSPROFILE'])
if ($strDocsAndSettings !== null ) {
$strAll = $strDocsAndSettings . 'All Users';
}//function getAllUsers()
* Returns the path to the application data directory.
* This is the directory in which applications save their
* On Windows, this is an own directory called "Application data",
* on *nix, the home directory is used.
* MacOS X has two application settings directories:
* - $HOME/Library/Preferences/<pref_file> for normal settings
* - $HOME/Library/Application Support/<app_name>/ for data files that
* that the application needs to store
* This method returns the latter, as it works for both storing just
* prefs and files in an application specific subdir. That's not 100%
* correct for apple, but it will work.
* @return string The application data directory
* win2k/xp: user_dir/names
* win98: C:\windows\Anwendungsdaten|appdata
if ($strAppData == null ) {
* We didn't find it in the user directory,
* so check the windows dir - win9x had the
* Returns the path to the user's desktop.
* @return string The user's desktop
if ($strDesktop !== false && file_exists($strDesktop)) {
* Returns the path to the user's documents directory.
* (normally below the home folder)
* @return string The "documents" directory
if ($strDocuments !== false && file_exists($strDocuments)) {
}//function getDocuments()
* Returns the path to the user's home directory.
* @return string The user's home directory
if (isset ($_ENV['HOME'])) {
//environment variable set
$strHome = $_ENV['HOME'];
//env not set, so try the default directory
if ($strUser == 'root') {
} else if ($strUser !== null ) {
$strHome = '/home/' . $strUser;
if (isset ($_ENV['HOME'])) {
//environment variable set
$strHome = $_ENV['HOME'];
//env not set, so try the default directory
$strHome = '/Users/' . $strUser;
$arEnv = $_SERVER + $_ENV;
if (isset ($arEnv['USERPROFILE'])) {
$strHome = $arEnv['USERPROFILE'];
} else if (isset ($arEnv['HOMEPATH']) && isset ($arEnv['HOMEDRIVE'])) {
$strHome = $arEnv['HOMEDRIVE'] . $arEnv['HOMEPATH'];
//It seems as if the german version of windows is the only
//one that translated "docs and settings". All other languages
* Returns the path to the programs directory.
* This is the dir where all programs are installed
* On windows, it's mostly "C:\Programs\", on linux,
* the /opt/ directory is returned.
* @return string The programs directory
$strPrograms = '/Applications/';
if ($strPrograms === false || !file_exists($strPrograms)) {
$arEnv = $_SERVER + $_ENV;
if (isset ($arEnv['ProgramFiles'])) {
$strPrograms = $arEnv['ProgramFiles'];
}//function getPrograms()
* Returns the path to the directory for temporary files.
* @return string The temporary directory
$arEnv = $_SERVER + $_ENV;
if (isset ($arEnv['TEMP'])) {
$strTemp = $arEnv['TEMP'];
} else if (isset ($arEnv['TMP'])) {
$strTemp = $arEnv['TMP'];
* Returns the path to the shared documents directory.
* Supports windows only (at least for now) as no other
* operating system seems to have such a folder.
* Returns NULL on failure (not windows or not found).
* @return string The shared documents dir
}//function getSharedDocuments()
* Returns the name of the guesses system.
* Can be compared with SYS_* constants.
* @return string The detected system
* Returns the name for the user
* under which name the program runs.
* This function returns the *system user name*,
* not the name with forename and surname
* On unix, this would be e.g. 'fbar' or so for
* This method is used my most other methods, so
* recognizing the user name is really important.
* Be sure to check this method if the others fail.
* @return string The user name
if (isset ($_ENV['USER'])) {
$strUser = $_ENV['USER'];
$strUser = trim(`whoami` );
$arEnv = $_SERVER + $_ENV;
if (isset ($arEnv['USERNAME'])) {
$strUser = $arEnv['USERNAME'];
}//function getUserName()
* Returns the windows directory (if any).
* NULL is returned if the system is not Windows.
* @return string The windows directory, NULL if not on windows
$arEnv = $_SERVER + $_ENV;
if (isset ($arEnv['SystemRoot'])) {
$strWindows = $arEnv['SystemRoot'];
} else if (isset ($arEnv['windir'])) {
$strWindows = $arEnv['windir'];
Documentation generated on Fri, 21 Dec 2012 15:30:03 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|