Core\Path::parent
Core\Path::parent(string $path, {levels?: uint}): stringCore\Path::parent( string $path, {levels?: uint}, ): string
Returns the folder that contains a path.
The method reads the path as text and never looks at the disk. It accepts / and \ as separators on every platform. The result is written with Core\Path::SEPARATOR, which is \ on Windows and / everywhere else.
The option levels says how many parts to remove from the end. The default is 1. With levels: 2, /srv/shop/config/app.toml gives /srv/shop. When there is nothing left to remove, the result is the root for an absolute path and . for a relative one. So the result is always a folder you can use.
Good to know: parent("") is .. levels: 0 returns the path with its separators cleaned up.
The examples below find the folder of a file, go up several levels, and find a folder next to a configuration file.
Parameters
$pathstringThe path./and\are both separators on every platform.
Options
levelsuintoptionalHow many parts to remove from the end. The default is1. With0, the result is the path with its separators cleaned up. If the number is larger than the count of parts, the result is the root, or.for a relative path.
Return value
Returns string. The folder, written with Path::SEPARATOR. When nothing is left, the result is the root for an absolute path and . for a relative one. parent('') is ..
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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
The folder of a file
<?nvs// Shows how to get the folder that contains a file with `Core\Path::parent`.
// On Windows, `parent` writes `\`. This example replaces it with `/`,// so the output is the same on every platform.class Show{ public static function folder(string $path): string { return Core\Str::replace(Core\Path::parent($path), Core\Path::SEPARATOR, '/'); }}
// This prints `/srv/shop/invoices`.echo Show::folder('/srv/shop/invoices/invoice-1042.pdf'), "\n";
// A file name with no folder is in the current folder. This prints `.`.echo Show::folder('report.pdf'), "\n";
// The root is its own folder. This prints `/`.echo Show::folder('/'), "\n";/srv/shop/invoices./Going up several levels
<?nvs// Shows how the `levels` option goes up more than one folder.
string $path = '/srv/shop/public/images/logo.png';
// `levels` is the number of parts removed from the end. The result stops at the root.// This prints `/srv/shop/public/images`, `/srv/shop/public`, `/srv/shop` and `/`.array<uint> $counts = [1, 2, 3, 9];foreach ($counts as uint $levels) { string $folder = Core\Path::parent($path, { levels: $levels }); echo Core\Str::replace($folder, Core\Path::SEPARATOR, '/'), "\n";}/srv/shop/public/images/srv/shop/public/srv/shop/A folder next to the config file
<?nvs// Shows how to find the log folder of an application from the path of its configuration file.
class App{ // The configuration file is at `<app>/config/app.toml`. // Two levels up is the application folder, and the logs are in `storage/logs` inside it. public static function logFolder(string $configFile): string { string $app = Core\Path::parent($configFile, { levels: 2 }); return Core\Path::join($app, 'storage', 'logs'); }}
// On Windows, the result uses `\`. This line replaces it with `/`,// so the output is the same on every platform. This prints `/srv/shop/storage/logs`.echo Core\Str::replace(App::logFolder('/srv/shop/config/app.toml'), Core\Path::SEPARATOR, '/'), "\n";/srv/shop/storage/logs