Skip to content

Core\Path::isAbsolute

Core\Path::isAbsolute(string $path): boolCore\Path::isAbsolute(
    string $path,
): bool

Checks whether a path starts at a root. It returns true or false, and it never looks at the disk.

A root is where a path starts on its own, without a current folder. There are three kinds: a separator such as /srv, a drive followed by a separator such as C:\Users, and a network share such as \\server\share. A path that starts at a root is absolute. Every other path is relative, and the empty path is relative too.

The check is the same on every platform, and it reads both / and \ as separators. So C:\Users is absolute on Linux too. C:report.txt is relative, because no separator follows the drive.

The examples below check a few paths, show the Windows forms, and turn a relative path from a configuration file into a full one.

Parameters

  • $pathstringThe path. / and \ are both separators on every platform.

Return value

Returns bool. true for /tmp, \tmp, C:/log and \\server\share. false for a relative path such as logs/app.log, for C:log, and for the empty string.

Examples

Where a path starts

<?nvs
// Shows how to check whether a path starts at a root.
class Show
{
public static function check(string $path): void
{
echo "\"", $path, "\": ", Core\Path::isAbsolute($path) ? 'absolute' : 'relative', "\n";
}
}
// A path that starts with `/` is absolute. This prints `absolute`.
Show::check('/srv/shop/logo.png');
// These two start in the current folder. Both print `relative`.
Show::check('uploads/logo.png');
Show::check('../logo.png');
// The empty path is relative. This prints `relative`.
Show::check('');
Output
"/srv/shop/logo.png": absolute
"uploads/logo.png": relative
"../logo.png": relative
"": relative

Drives and network shares

<?nvs
// Shows that `Core\Path::isAbsolute` reads Windows paths the same way on every platform.
class Show
{
public static function check(string $path): void
{
echo $path, ': ', Core\Path::isAbsolute($path) ? 'absolute' : 'relative', "\n";
}
}
// A drive followed by a separator is a root. Both print `absolute`.
Show::check("C:\\Users\\report.txt");
Show::check('C:/Users/report.txt');
// No separator follows the drive, so this path is relative. This prints `relative`.
Show::check('C:report.txt');
// A network share is a root. This prints `absolute`.
Show::check("\\\\server\\share\\report.txt");
// A path that starts with `\` is absolute too. This prints `absolute`.
Show::check("\\Users\\report.txt");
Output
C:\Users\report.txt: absolute
C:/Users/report.txt: absolute
C:report.txt: relative
\\server\share\report.txt: absolute
\Users\report.txt: absolute

A folder from the settings

<?nvs
// Shows how to turn a folder from a configuration file into a full path.
class Settings
{
// A relative folder is read from the folder of the application. An absolute one is used as it is.
public static function folder(string $app, string $configured): string
{
if (Core\Path::isAbsolute($configured)) {
return $configured;
}
return Core\Path::join($app, $configured);
}
}
// On Windows, `Core\Path::join` writes `\`. This example replaces it with `/`,
// so the output is the same on every platform.
string $app = '/srv/shop';
// This prints `/srv/shop/storage/logs`.
echo Core\Str::replace(Settings::folder($app, 'storage/logs'), Core\Path::SEPARATOR, '/'), "\n";
// This prints `/var/log/shop`.
echo Core\Str::replace(Settings::folder($app, '/var/log/shop'), Core\Path::SEPARATOR, '/'), "\n";
Output
/srv/shop/storage/logs
/var/log/shop