Home PHP Filesystem basename() PHP basename() Function Definition In PHP, basename() returns the last part of a path, which is usually the file name. For example, the basename of /var/www/index.php is index.php.
You can pass a suffix such as .php as the second argument and basename() will remove it from the end of the result. It only works with the text of the path, so the file does not have to exist.
Syntax Call basename() with the values you need. Start with this line.
$file = basename("/var/www/html/index.php", ".php"); Call What it does basename("/var/www/html/index.php")Returns index.php, the last part of the path. basename("/var/www/html/index.php", ".php")Returns index because the .php suffix is removed. basename("/var/www/images/")Returns images because a trailing slash is ignored.
basename() is locale aware, so on systems with a multibyte locale you may need setlocale() to handle special characters correctly. It never checks the disk, so it works for paths that do not exist.
Library declarations basename(string $path, string $suffix = ""): string Parameters Parameter Description path Required. The path as a string, for example a file path or a URL path. suffix Optional. If the name ends with this suffix, the suffix is removed from the result.
Return Value Return Value Description string The last component of the path, with the suffix removed if one was given.
PHP Examples 1. Get the File Name from a Path 2. Trailing Slash and Empty Paths 3. List File Names from Several Paths Example 1 — Get the File Name from a Path Pull the file name out of a full path, with and without the extension.
<?php
$path = "/var/www/html/index.php";
echo basename($path), PHP_EOL;
echo basename($path, ".php"), PHP_EOL; How It Works We store a full path in $path. basename($path) returns only the last part, index. php.basename($path, ". php") removes the .php suffix from the end.PHP_EOL adds a line break after each result. Example 2 — Trailing Slash and Empty Paths See what basename() returns for folders, the root, and an empty string.
<?php
echo "[", basename("/var/www/images/"), "]", PHP_EOL;
echo "[", basename("/"), "]", PHP_EOL;
echo "[", basename(""), "]", PHP_EOL;
echo "[", basename("report.pdf", "report.pdf"), "]", PHP_EOL; [images]
[]
[]
[report.pdf] How It Works A trailing slash is ignored, so the folder name images is returned. The root path / has no name, so the result is an empty string. An empty path also gives an empty string. If the suffix is the whole name, basename() keeps the name instead of returning an empty string. Frequently Asked Questions What does basename() do in PHP? It returns the last component of a path, which is normally the file name. It works on the text of the path and does not check the disk.
How do I remove the file extension with basename()? Pass the extension as the second argument, for example basename($path, ".php"). It removes the suffix only if the name ends with it.
Does basename() need the file to exist? No. It only reads the string you give it, so it works for paths that do not exist.
What is the difference between basename() and dirname()? basename() returns the last part of the path (the file name), while dirname() returns everything before it (the folder).
Is there a better way to get the name without extension? Yes. pathinfo($path, PATHINFO_FILENAME) is safer when the extension can vary, because you do not need to know it in advance.
Next: chgrp() chgrp() changes the group that owns a file or folder. You can give the group as a name lik.
chgrp() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion