Skip to content

Core\IO::canonicalize

Core\IO::canonicalize(string $path): stringCore\IO::canonicalize(
    string $path,
): string

Gives the full, absolute path of a file or folder that exists.

Core\IO::canonicalize removes every . and .. from the path and follows every symbolic link. The result is the one path the system uses for that file. Two different ways to write the path of one file give the same result. So you can compare the results to check whether two paths name the same file.

Every part of the path must exist. For a path with nothing at it, canonicalize throws an IOError.

A program you start from the command line may read any path. On a server, a program may read only the files in its application folder, except nvs.toml and names that start with .. Any other path needs the fs.read capability. Without it, canonicalize throws a RuntimeError.

Good to know: canonicalize does not check that a path stays inside a folder. To check a path that a user sent, use Core\IO::within.

Parameters

  • $pathstringThe path to resolve. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it. Every part of the path must exist, including the last one.

Return value

Returns string. The resolved absolute path. Resolving the result again gives the same path, so you can compare two results for equality. Core\Path::normalize cannot do this. Two different ways to write the path of one file normalize to different strings. They canonicalize to the same string.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path.
  • IOErrorThe capability allowed the call, but the operating system did not. A part of the path does not exist or is not a directory, or a symbolic link loops. A missing path cannot be resolved, so this method throws an error. exists returns false for it.

Examples

Two paths to one file

<?nvs
// Shows that two different ways to write the path of one file give the same result.
string $dir = Core\IO::createTemporaryDirectory();
Core\IO::makeDirectory(Core\Path::join($dir, 'docs'));
Core\IO::write(Core\Path::join($dir, 'readme.txt'), 'hello');
string $short = Core\Path::join($dir, 'readme.txt');
string $long = Core\Path::join($dir, 'docs/../././readme.txt');
// The two texts are different, but the results are equal. This prints "the same file".
if (Core\IO::canonicalize($short) == Core\IO::canonicalize($long)) {
echo 'the same file', "\n";
}
// The result has no `..` in it. This prints "readme.txt".
echo Core\Path::fileName(Core\IO::canonicalize($long)), "\n";
Output
the same file
readme.txt

A path that does not exist

<?nvs
// Shows what `Core\IO::canonicalize` does with a path that has nothing at it.
string $missing = Core\Path::join(Core\IO::createTemporaryDirectory(), 'not-here.txt');
// `Core\IO::exists` returns `false` for this path. This prints "false".
echo Core\IO::exists($missing) ? 'true' : 'false', "\n";
// `canonicalize` throws an `IOError`, because a missing file has no full path. This prints "no such file".
try {
echo Core\IO::canonicalize($missing), "\n";
} catch (IOError $error) {
echo 'no such file', "\n";
}
Output
false
no such file

Load each config file once

<?nvs
// Shows how a program loads each settings file only once, even when a list names one file in two ways.
string $dir = Core\IO::createTemporaryDirectory();
Core\IO::makeDirectory(Core\Path::join($dir, 'conf'));
Core\IO::write(Core\Path::join($dir, 'conf/app.ini'), "name = shop\n");
Core\IO::write(Core\Path::join($dir, 'conf/db.ini'), "host = localhost\n");
// Three entries, but the first two name the same file.
array<string> $includes = [
Core\Path::join($dir, 'conf/app.ini'),
Core\Path::join($dir, 'conf/../conf/app.ini'),
Core\Path::join($dir, 'conf/db.ini'),
];
// The full path is the key. A second path to a file that is already loaded finds its key.
array<bool> $loaded = [];
foreach ($includes as string $include) {
string $key = Core\IO::canonicalize($include);
if (Core\Arr::hasKey($loaded, $key)) {
continue;
}
$loaded[$key] = true;
echo Core\IO::read($include);
}
// This prints the two settings lines, and then "3 entries, 2 files".
echo Core\Str::format('%d entries, %d files', Core\Arr::count($includes), Core\Arr::count($loaded)), "\n";
Output
name = shop
host = localhost
3 entries, 2 files