Skip to content

Core\Path::relativeTo

Returns the path that leads from one folder to a file or another folder. The method works on the text of the paths and never looks at the disk.

The result goes up with .. out of each folder the two paths do not share, and then down to the place you asked for. From /srv/shop/views, the path to /srv/shop/logs/app.log is ../logs/app.log. When both paths name the same place, the result is .. Both paths have their . and .. parts removed first, as Core\Path::normalize does.

The result is null when no relative path exists. That happens when one path is absolute and the other is not, or when they start at different roots, such as two drives. It also happens when $base starts with more .. parts than the other path.

The examples below find a relative path, show when the result is null, and write the links between the pages of a website.

Parameters

  • $pathstringThe place the result leads to.
  • $basestringThe folder the result starts from. The result has one .. for each folder of $base that $path does not share.

Return value

Returns ?string. A relative path, written with Core\Path::SEPARATOR. It is . when both paths name the same place. It is null when no relative path exists. That happens when one path is absolute and the other is not, or when the paths start at different roots. It also happens when $base starts with more .. parts than $path does. Folder names are compared byte for byte. A drive letter and a server name ignore upper and lower case.

Examples

From one folder to another

<?nvs
// Shows how to find the path that leads from a folder to a file.
// On Windows, the result is written with `\`. This example replaces it with `/`,
// so the output is the same on every platform.
class Show
{
public static function way(string $path, string $base): void
{
string $way = Core\Path::relativeTo($path, $base) ?? 'null';
echo Core\Str::replace($way, Core\Path::SEPARATOR, '/'), "\n";
}
}
// The file is in the folder. This prints `index.nvs`.
Show::way('/srv/shop/index.nvs', '/srv/shop');
// The result goes up twice and then down. This prints `../../log/app.log`.
Show::way('/var/log/app.log', '/var/www/html');
// Both paths name the same folder. This prints `.`.
Show::way('/srv/shop', '/srv/shop/');
// Relative paths work too. This prints `images/logo.png`.
Show::way('shop/images/logo.png', 'shop');
Output
index.nvs
../../log/app.log
.
images/logo.png

When there is no way

<?nvs
// Shows when `Core\Path::relativeTo` returns `null`, because no relative path exists.
class Show
{
public static function way(string $path, string $base): void
{
string $way = Core\Path::relativeTo($path, $base) ?? 'null';
echo Core\Str::replace($way, Core\Path::SEPARATOR, '/'), "\n";
}
}
// One path is absolute and the other is relative. This prints `null`.
Show::way('/srv/shop', 'srv/shop');
// The paths are on two different drives. This prints `null`.
Show::way("C:\\shop\\logo.png", "D:\\shop");
// A drive letter can be upper or lower case. This prints `logo.png`.
Show::way("C:\\shop\\logo.png", "c:\\shop");
// The name of the folder above `..` is not in the path. This prints `null`.
Show::way('logo.png', '..');
Output
null
null
logo.png
null

Links between pages

<?nvs
// Shows how to write a link from one page of a website to another.
// The links still work when the whole website moves to another folder.
class Links
{
public static function to(string $from, string $to): string
{
// A link starts in the folder of the page it is on.
string $way = Core\Path::relativeTo($to, Core\Path::parent($from)) ?? $to;
string $href = Core\Str::replace($way, Core\Path::SEPARATOR, '/');
return "<a href=\"{$href}\">" . Core\Path::fileName($to, { withoutExtension: true }) . '</a>';
}
}
// This prints `<a href="install.html">install</a>`.
echo Links::to('guide/start.html', 'guide/install.html'), "\n";
// This prints `<a href="../api/path.html">path</a>`.
echo Links::to('guide/start.html', 'api/path.html'), "\n";
// This prints `<a href="../../index.html">index</a>`.
echo Links::to('guide/linux/setup.html', 'index.html'), "\n";
Output
<a href="install.html">install</a>
<a href="../api/path.html">path</a>
<a href="../../index.html">index</a>