Skip to content

Core\Path::extension

Core\Path::extension(string $path): ?stringCore\Path::extension(
    string $path,
): ?string

Returns the extension of a file name: the text after the last dot, without the dot.

The method reads the last part of the path as text and never looks at the disk. invoice.pdf gives pdf, and backup.tar.gz gives gz. The extension keeps its letter case, so photo.JPG gives JPG.

When there is no extension, the result is null. A name without a dot has none. A name that starts with a dot, such as .gitignore, has none, because the dot is part of the name. A name that ends with a dot, such as report., has none too.

The result is exactly what Core\Path::withExtension takes, so you can read an extension and write it back.

The examples below read extensions from several paths, show the names that have none, and choose a content type for a file.

Parameters

  • $pathstringThe path. / and \ are both separators on every platform.

Return value

Returns ?string. The extension, or null when there is none. A name that starts with a dot, such as .gitignore, has none. A name that ends with a dot, such as report., has none too.

Examples

Read an extension

<?nvs
// Shows how to read the extension of a file with `Core\Path::extension`.
// The dot is not part of the result. This prints `pdf`.
echo Core\Path::extension('/srv/shop/invoices/invoice-1042.pdf'), "\n";
// Only the text after the last dot is the extension. This prints `gz`.
echo Core\Path::extension('backups/database.tar.gz'), "\n";
// The letter case stays as it is. This prints `JPG`.
echo Core\Path::extension("C:\\Photos\\IMG_0042.JPG"), "\n";
Output
pdf
gz
JPG

Names with no extension

<?nvs
// Shows the names that have no extension. For each of them, the result is `null`.
array<string> $paths = [
'/usr/bin/php',
'/home/sam/.gitignore',
'drafts/report.',
'/srv/shop/',
];
// `??` replaces `null` with the text after it. This prints `(none)` four times.
foreach ($paths as string $path) {
echo Core\Path::extension($path) ?? '(none)', "\n";
}
Output
(none)
(none)
(none)
(none)

Choose a content type

<?nvs
// Shows how to choose the content type of a file from its extension, before the file is sent.
class Download
{
public static function contentType(string $path): string
{
// `Core\Str::lower` makes `PDF` and `pdf` the same.
// A file with no extension gets the general type for bytes.
?string $extension = Core\Path::extension($path);
if ($extension == null) {
return 'application/octet-stream';
}
return match (Core\Str::lower($extension)) {
'pdf' => 'application/pdf',
'jpg', 'jpeg' => 'image/jpeg',
'png' => 'image/png',
'csv' => 'text/csv',
default => 'application/octet-stream',
};
}
}
// This prints `application/pdf`, `image/jpeg`, `text/csv` and `application/octet-stream`.
echo Download::contentType('invoices/invoice-1042.PDF'), "\n";
echo Download::contentType('uploads/holiday.jpeg'), "\n";
echo Download::contentType('exports/orders-2026-09.csv'), "\n";
echo Download::contentType('bin/backup'), "\n";
Output
application/pdf
image/jpeg
text/csv
application/octet-stream