Skip to content

Instantly share code, notes, and snippets.

@zerolab
Last active December 15, 2015 04:19
Show Gist options
  • Save zerolab/5200481 to your computer and use it in GitHub Desktop.
Save zerolab/5200481 to your computer and use it in GitHub Desktop.
Detect the user's home dir on Linux/Windows
<?php
/**
* Get the home directory
*
* @see http://techblog.zabuchy.net/2012/how-to-detect-user-home-directory-php-linux-windows/
*/
function homedir() {
// getenv('HOME') isn't set on windows and generates a Notice.
$home = getenv('HOME');
if (empty($home)) {
if (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
// home on windows
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
}
}
return empty($home) ? NULL : $home;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment