Skip to content

Core\Path::fileName

Core\Path::fileName(string $path, {withoutExtension?: bool}): stringCore\Path::fileName(
    string $path,
    {withoutExtension?: bool},
): string

Returns the last part of a path: the file name, or the name of the last folder.

The method reads the path as text and never looks at the disk. It accepts / and \ as separators on every platform, so C:\reports\may.pdf and /srv/reports/may.pdf both give may.pdf. A separator at the end is ignored, so /srv/reports/ gives reports.

A path that is only a root, such as / or C:\, has no name. The result is the empty string.

With the option withoutExtension: true, the method also removes the extension, so may.pdf gives may. A name that starts with a dot, such as .gitignore, keeps its whole name.

The examples below read names from several kinds of path, remove an extension, and take the file name from an uploaded file.

Parameters

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

Options

  • withoutExtensionbooloptionalWhen true, the extension is removed, so report.pdf gives report. The default is false.

Return value

Returns string. The name. For a path that is only a root, such as / or C:\, the result is the empty string.

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

The last part of a path

<?nvs
// Shows how to get the last part of a path with `Core\Path::fileName`.
// A file name. This prints `invoice-1042.pdf`.
echo Core\Path::fileName('/srv/shop/invoices/invoice-1042.pdf'), "\n";
// A folder with a separator at the end. The separator is ignored, so this prints `invoices`.
echo Core\Path::fileName('/srv/shop/invoices/'), "\n";
// A Windows path works on every platform. This prints `notes.txt`.
echo Core\Path::fileName("C:\\Users\\sam\\notes.txt"), "\n";
// A root has no name, so the result is the empty string. This prints `[]`.
echo '[', Core\Path::fileName('/'), "]\n";
Output
invoice-1042.pdf
invoices
notes.txt
[]

A name without its extension

<?nvs
// Shows how to get a file name without its extension.
array<string> $paths = [
'/var/log/shop/orders.log',
'backups/database.tar.gz',
'/home/sam/.gitignore',
'README',
];
// `withoutExtension: true` removes the last dot and the text after it, so `database.tar.gz` gives `database.tar`.
// A name that starts with a dot keeps its whole name.
foreach ($paths as string $path) {
echo Core\Path::fileName($path, { withoutExtension: true }), "\n";
}
Output
orders
database.tar
.gitignore
README

The name of an uploaded file

<?nvs
// Shows how to keep only the file name that a browser sends with an upload.
class Upload
{
// Some browsers send the whole path from the user's computer.
// `fileName` keeps only the last part, so no folder names reach your storage.
public static function storedName(string $sentName): string
{
string $name = Core\Path::fileName($sentName);
if ($name == '' || $name == '.' || $name == '..') {
return 'upload.bin';
}
return $name;
}
}
// This prints `holiday.jpg`, `report.pdf`, `passwd` and `upload.bin`.
echo Upload::storedName('holiday.jpg'), "\n";
echo Upload::storedName("C:\\Users\\sam\\Documents\\report.pdf"), "\n";
echo Upload::storedName('../../etc/passwd'), "\n";
echo Upload::storedName('../'), "\n";
Output
holiday.jpg
report.pdf
passwd
upload.bin