Skip to content

Core\Path::withExtension

Returns a path with the extension of its last part changed.

The method works on the text of the path and never looks at the disk. orders.csv with xlsx gives orders.xlsx. A name without an extension gets one, so notes with txt gives notes.txt. When the new extension is null, the old one is removed.

You write the extension without its dot, the same way Core\Path::extension returns it. An extension may have a dot inside, such as tar.gz. An empty extension, one that starts with a dot and one with a / or \ in it throw a RuntimeError. A path with no file name, such as / or the empty string, throws a RuntimeError too.

The result is written with Core\Path::SEPARATOR, which is \ on Windows and / everywhere else.

The examples below change an extension, remove one, and name the converted copy of an uploaded image.

Parameters

  • $pathstringThe path. / and \ are both separators on every platform.
  • $extension?stringThe new extension, without its dot, the same way Core\Path::extension returns it. It may have a dot inside, such as tar.gz. null removes the extension.

Return value

Returns string. The new path, written with Core\Path::SEPARATOR.

Errors

  • RuntimeError$extension is empty, starts with a ., or contains / or \. Or $path has no file name, such as / or ''.

Examples

Change an extension

<?nvs
// Shows how to change the extension of a file name with `Core\Path::withExtension`.
// On Windows, `withExtension` writes `\`. This example replaces it with `/`,
// so the output is the same on every platform.
class Show
{
public static function renamed(string $path, string $extension): string
{
return Core\Str::replace(Core\Path::withExtension($path, $extension), Core\Path::SEPARATOR, '/');
}
}
// This prints `/srv/shop/exports/orders.xlsx`.
echo Show::renamed('/srv/shop/exports/orders.csv', 'xlsx'), "\n";
// A name with no extension gets one. This prints `notes.txt`.
echo Show::renamed('notes', 'txt'), "\n";
// Only the last extension is replaced. This prints `photo.small.jpg`.
echo Show::renamed('photo.small.JPG', 'jpg'), "\n";
Output
/srv/shop/exports/orders.xlsx
notes.txt
photo.small.jpg

Remove an extension

<?nvs
// Shows how to remove an extension, and how to write an extension with two parts.
// `null` removes the last extension. This prints `backup.tar`.
echo Core\Path::withExtension('backup.tar.gz', null), "\n";
// An extension may have a dot inside. This prints `database.tar.gz`.
echo Core\Path::withExtension('database', 'tar.gz'), "\n";
// A name that starts with a dot has no extension, so nothing is removed.
// This prints `.gitignore`.
echo Core\Path::withExtension('.gitignore', null), "\n";
// The extension is written without its dot. `.md` throws a `RuntimeError`.
try {
echo Core\Path::withExtension('readme.txt', '.md'), "\n";
} catch (RuntimeError $error) {
echo 'Write `md`, not `.md`.', "\n";
}
Output
backup.tar
database.tar.gz
.gitignore
Write `md`, not `.md`.

Name a converted image

<?nvs
// Shows how to name the converted copy of an uploaded image, before the copy is written.
class Thumbnails
{
public static function pathFor(string $upload): string
{
// The copy keeps the name of the upload and gets the `webp` extension.
string $name = Core\Path::withExtension(Core\Path::fileName($upload), 'webp');
return '/srv/shop/thumbnails/' . $name;
}
}
// This prints `/srv/shop/thumbnails/holiday.webp` and `/srv/shop/thumbnails/logo.webp`.
echo Thumbnails::pathFor('uploads/2026/09/holiday.jpeg'), "\n";
echo Thumbnails::pathFor('uploads/logo.png'), "\n";
Output
/srv/shop/thumbnails/holiday.webp
/srv/shop/thumbnails/logo.webp