PHP basename() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

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.

PHP
$file = basename("/var/www/html/index.php", ".php");
CallWhat 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
PHP
basename(string $path, string $suffix = ""): string

Parameters

ParameterDescription
pathRequired. The path as a string, for example a file path or a URL path.
suffixOptional. If the name ends with this suffix, the suffix is removed from the result.

Return Value

Return ValueDescription
stringThe last component of the path, with the suffix removed if one was given.

PHP Examples

Example 1 — Get the File Name from a Path

Pull the file name out of a full path, with and without the extension.

PHP
<?php
$path = "/var/www/html/index.php";

echo basename($path), PHP_EOL;
echo basename($path, ".php"), PHP_EOL;

How It Works

  1. We store a full path in $path.
  2. basename($path) returns only the last part, index. php.
  3. basename($path, ". php") removes the .php suffix from the end.
  4. 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
<?php
echo "[", basename("/var/www/images/"), "]", PHP_EOL;
echo "[", basename("/"), "]", PHP_EOL;
echo "[", basename(""), "]", PHP_EOL;
echo "[", basename("report.pdf", "report.pdf"), "]", PHP_EOL;

How It Works

  1. A trailing slash is ignored, so the folder name images is returned.
  2. The root path / has no name, so the result is an empty string.
  3. An empty path also gives an empty string.
  4. If the suffix is the whole name, basename() keeps the name instead of returning an empty string.

Example 3 — List File Names from Several Paths

Loop over a list of paths and print only the file names.

PHP
<?php
$paths = [
    "/home/user/docs/resume.pdf",
    "/home/user/photos/cat.jpg",
    __DIR__ . "/demo.txt",
];

foreach ($paths as $path) {
    echo basename($path), PHP_EOL;
}

How It Works

  1. The array holds three paths, and the last one is built with __DIR__.
  2. foreach goes through each path one at a time.
  3. basename() removes the folder part from each path.
  4. Only the file names are printed.

Frequently Asked Questions

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.
Pass the extension as the second argument, for example basename($path, ".php"). It removes the suffix only if the name ends with it.
No. It only reads the string you give it, so it works for paths that do not exist.
basename() returns the last part of the path (the file name), while dirname() returns everything before it (the folder).
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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