Core\Path::join
Core\Path::join(string $base, string ...$segments): stringCore\Path::join( string $base, string ...$segments, ): string
Joins a path and any number of parts into one path, with one separator between each part.
The method works on the text of the paths and never looks at the disk. /srv/shop joined with uploads and logo.png gives /srv/shop/uploads/logo.png. Extra separators are removed, so /srv/shop/ and uploads give the same result as /srv/shop and uploads.
Only the first path decides where the result starts. A later part that starts with /, or with a drive such as C:\, is added after the first path and does not replace it. So /srv/shop joined with /etc/passwd gives /srv/shop/etc/passwd.
join does not remove ... A part that is .. stays in the result, and it still means the parent folder. Use Core\Path::normalize to resolve it.
The result is written with Core\Path::SEPARATOR, which is \ on Windows and / everywhere else.
The examples below join a folder and a file, show what happens to a part that starts with /, and build the path of a customer's upload.
Parameters
$basestringThe first path. The result starts where this path starts....$segmentsstringvariadicAny number of paths to add, in order. A leading/,\or drive in one of them is removed.
Return value
Returns string. The joined path, written with Core\Path::SEPARATOR. With no segments, the result is $base with its extra separators removed. A .. segment stays in the result and still means the parent folder.
Examples
A folder and a file
<?nvs// Shows how to join a folder and a file name with `Core\Path::join`.
// On Windows, `join` writes `\`. This example replaces it with `/`,// so the output is the same on every platform.class Show{ public static function slashes(string $path): string { return Core\Str::replace($path, Core\Path::SEPARATOR, '/'); }}
// This prints `/srv/shop/invoices/invoice-1042.pdf`.echo Show::slashes(Core\Path::join('/srv/shop', 'invoices', 'invoice-1042.pdf')), "\n";
// A separator at the end of the folder is not doubled. This prints `logs/app.log`.echo Show::slashes(Core\Path::join('logs/', 'app.log')), "\n";
// With no parts to add, the result is the first path. This prints `/srv/shop`.echo Show::slashes(Core\Path::join('/srv/shop/')), "\n";/srv/shop/invoices/invoice-1042.pdflogs/app.log/srv/shopA part that starts with a slash
<?nvs// Shows that only the first path decides where the result of `Core\Path::join` starts.
class Show{ public static function slashes(string $path): string { return Core\Str::replace($path, Core\Path::SEPARATOR, '/'); }}
// The second part starts with `/`, and it is added after the first path.// This prints `/srv/shop/uploads/etc/passwd`.echo Show::slashes(Core\Path::join('/srv/shop/uploads', '/etc/passwd')), "\n";
// A drive is added the same way. This prints `/srv/shop/uploads/Windows`.echo Show::slashes(Core\Path::join('/srv/shop/uploads', "C:\\Windows")), "\n";
// `..` stays in the result. This prints `/srv/shop/uploads/../config`.// `Core\Path::normalize` then gives `/srv/shop/config`.string $up = Core\Path::join('/srv/shop/uploads', '..', 'config');echo Show::slashes($up), "\n";echo Show::slashes(Core\Path::normalize($up)), "\n";/srv/shop/uploads/etc/passwd/srv/shop/uploads/Windows/srv/shop/uploads/../config/srv/shop/configThe path of an upload
<?nvs// Shows how to build the path where a customer's upload is stored.
class Uploads{ public static function pathFor(string $root, int $customerId, string $fileName): string { // Each customer has a folder named after their number. return Core\Path::join($root, 'customers', "{$customerId}", $fileName); }}
// On Windows, `join` writes `\`. This example replaces it with `/`.// This prints `/srv/shop/storage/customers/1042/receipt.pdf`.string $path = Uploads::pathFor('/srv/shop/storage', 1042, 'receipt.pdf');echo Core\Str::replace($path, Core\Path::SEPARATOR, '/'), "\n";/srv/shop/storage/customers/1042/receipt.pdf