PHP basename() Function
The PHP basename() function returns the filename from a string containing the path to a file or directory. This function will return the trailing name component.
Syntax
basename(path, suffix)
Parameters
path |
Required. Specify a file path. On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/). |
suffix |
Optional. Specify the file extension. If the filename has this file extension, the file extension will be cut off. |
Return Value
Returns the filename from the given path to a file or directory.
Example:
The example below shows the usage of basename() function.
<?php echo "1) ".basename("/etc/test.txt", ".txt")."\n"; echo "2) ".basename("/etc/test.txt")."\n"; echo "3) ".basename("/etc/demo")."\n"; echo "4) ".basename("/etc/")."\n"; echo "5) ".basename(".")."\n"; echo "6) ".basename("/"); ?>
The output of the above code will be:
1) test 2) test.txt 3) demo 4) etc 5) . 6)
❮ PHP Filesystem Reference