Skip to content

Instantly share code, notes, and snippets.

@aklump
Created May 9, 2026 14:14
Show Gist options
  • Select an option

  • Save aklump/56425150ff346b229adab691cf36f307 to your computer and use it in GitHub Desktop.

Select an option

Save aklump/56425150ff346b229adab691cf36f307 to your computer and use it in GitHub Desktop.
<?php
namespace AKlump\Knowledge\Helpers;
/**
* Retrieves the home directory of the current user.
*
* The method first attempts to fetch the home directory using the `HOME`
* or `USERPROFILE` environment variables. If neither is set, it combines
* the `HOMEDRIVE` and `HOMEPATH` environment variables to determine the
* home directory. If no home directory can be determined, it returns null.
*
* @return string|null The home directory path, or null if it cannot be determined.
*/
class GetUserHomeDirectory {
public function __invoke() {
$home = getenv('HOME') ?: getenv('USERPROFILE');
if ($home) {
return rtrim($home, DIRECTORY_SEPARATOR);
}
$home_drive = getenv('HOMEDRIVE');
$home_path = getenv('HOMEPATH');
if ($home_drive && $home_path) {
return rtrim($home_drive . $home_path, DIRECTORY_SEPARATOR);
}
return NULL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment