PHP 4, PHP 5, PHP 7, PHP 8
basename - Returns trailing name component of path

basename( string$path, [string$suffix = ""] ): string

Given a string containing the path to a file or directory, this function will return the trailing name component.


basename operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".


basename is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale function. If path contains characters which are invalid for the current locale, the behavior of basename is undefined.



A path.

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).


If the name component ends in suffix this will also be cut off.

Return Values

Returns the base name of the given path.

Related Functions

Example of basename

Show all examples for basename

PHP Version: