Skip to content

Core\Path::fromWorkingDirectory

Core\Path::fromWorkingDirectory(string $path): stringCore\Path::fromWorkingDirectory(
    string $path,
): string

Joins a path to the folder the program was started from, and returns the full path.

A command-line tool uses it for a file name the user typed, such as report.txt in nvs run tool.nvs report.txt. The user means a file in the folder they are working in, and fromWorkingDirectory gives the full path of that file. . and .. are removed from the result. A path that is already full is not joined to the folder. It is returned with . and .. removed.

Good to know: fromWorkingDirectory throws a RuntimeError while the program answers a web request. A server's working folder is not the folder of your app. Write the path in your program as a string in quotes instead. A path written in quotes starts at the folder of the file that contains it.

The examples below turn a typed file name into a full path and show that . and .. are removed. The last one builds export paths below the starting folder.

Parameters

  • $pathstringThe path. A relative path is joined to the working folder. A full path is returned as it is, with its . and .. parts removed.

Return value

Returns string. A full path, written with Core\Path::SEPARATOR, with its . and .. parts removed. The method does not check that the file exists. The result is not tainted, so you can pass a path from Core\Cli::arguments to Core\IO::read.

Errors

  • RuntimeErrorThe program is handling a web request. A server has no working folder that belongs to the app.

Changelog

VersionChange
0.0.6Core\Path::parent returns the folder of a path, and Core\Path::fileName returns its last part. fromWorkingDirectory and thisDirectory write their words in full. The old names were dirname, basename, fromCwd and thisDir.

Examples

A file name from the command line

<?nvs
// Shows how a command-line tool turns a file name the user typed into a full
// path. Start it with `nvs run 01-a-file-name-from-the-command-line.nvs notes.txt`.
array<tainted string> $words = Core\Cli::arguments();
tainted string $typed = Core\Arr::first($words) ?? 'notes.txt';
// `fromWorkingDirectory` joins the name to the folder the user started the program from.
// The result is a full path that is not `tainted`, so `Core\IO::read` accepts it.
string $path = Core\Path::fromWorkingDirectory($typed);
// The path starts at a root, so this prints `true`.
echo Core\Path::isAbsolute($path) ? 'true' : 'false', "\n";
// The file name is still the last part of the path. This prints `notes.txt`.
echo Core\Path::fileName($path), "\n";
Output
true
notes.txt

Dots in the typed path

<?nvs
// Shows that `fromWorkingDirectory` removes `.` and `..` from the path it returns.
string $start = Core\Path::fromWorkingDirectory('.');
string $path = Core\Path::fromWorkingDirectory('reports/../logs/./today.txt');
// `relativeTo` gives the part of `$path` after the start folder.
// `split` gives its parts, so this prints `logs, today.txt`.
array<string> $parts = Core\Path::split(Core\Path::relativeTo($path, $start) ?? '');
echo Core\Str::join($parts, ', '), "\n";
Output
logs, today.txt

An export folder for a command line tool

<?nvs
// Shows a command-line tool that puts its export files in a folder below the
// folder the user started it from.
string $exports = Core\Path::fromWorkingDirectory('exports');
// `Core\Path::join` adds one file name per month to the folder.
array<string> $months = ['2026-08', '2026-09'];
foreach ($months as string $month) {
string $file = Core\Path::join($exports, $month . '.csv');
// Each path is full. This prints the folder name and the file name.
echo Core\Path::isAbsolute($file) ? 'full: ' : 'relative: ';
echo Core\Path::fileName(Core\Path::parent($file)), ' ', Core\Path::fileName($file), "\n";
}
Output
full: exports 2026-08.csv
full: exports 2026-09.csv