basename()
This function will return the file name part of the given path. This is also known as the “base name” of the file.
Give it a second parameter and if the file name ends with that string, the function will cut off that portion before returning it to you. If the file name does not have that suffix, the whole name will be returned.
$path = '/home/admin/public_html/index.php'; $file = basename($path); // index.php $file = basename($path, '.php'); // index $file = basename($path, '.html'); // index.php // the suffix is not necessarily the file extension $file = basename($path, 'x.php'); // inde $file = basename($path, 'hp'); // index.p // works on files with no extension $path = '/home/admin/public_html/index'; $file = basename($path); // index
The path can also be a url but the result may not always be a file name as shown in these examples below. This isn’t the intended use and really only works sometimes as a side effect.
$path = 'http://example.com/file.php'; $file = basename($path); // file.php $path = 'http://example.com/file.php?q=monkey&y=dunno'; $file = basename($path); // file.php?q=monkey&y=toto $path = 'http://example.com/'; $file = basename($path); // example.com
See the manual entry for basename()
