Backend Development 5 min read

Using PHP basename() to Extract File Names from Paths

This article introduces PHP's basename() function, explains its syntax and parameters, and demonstrates how to extract filenames from absolute or relative paths—including optional suffix removal—through clear code examples while noting OS-specific path separator considerations.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP basename() to Extract File Names from Paths

In PHP programming, handling file paths is common, and the basename() function provides a quick way to retrieve the filename portion of a path. This article explains the function’s syntax, parameters, and behavior, and demonstrates its usage with several code examples.

string basename ( string $path [, string $suffix ] )

Parameters:

$path: required, the file path, which can be relative or absolute.

$suffix: optional, a file extension to be removed from the result.

Functionality:

Returns the filename part of the given path.

Below are several examples that illustrate how to use basename() :

Example 1:

$path = "/var/www/html/index.php";
$filename = basename($path);
echo $filename;

Output:

index.php

In this example, the absolute path "/var/www/html/index.php" is passed to basename() , and the function returns "index.php" , which is then printed.

Example 2:

$path = "images/pic.jpg";
$filename = basename($path);
echo $filename;

Output:

pic.jpg

This example shows that a relative path "images/pic.jpg" also yields the filename "pic.jpg" .

Example 3 (removing a suffix):

$path = "/var/www/html/index.php";
$filename = basename($path, ".php");
echo $filename;

Output:

index

Here, the optional $suffix parameter ".php" is provided, so basename() strips the suffix and returns "index" .

The function returns a string containing only the filename part of the path; if the path does not contain a filename, it returns ".".

Note that the result of basename() may be affected by the operating system: Windows uses backslashes ("\\") as separators, while Linux and macOS use forward slashes ("/"). Therefore, be aware of the separator when using the function across platforms.

Summary

In PHP development, the basename() function is a handy tool for extracting the filename from a path, useful in file operations, URL handling, and upload processing. Mastering this function can improve code readability and development efficiency.

Java learning material

C language learning material

Frontend learning material

C++ learning material

PHP learning material

BackendPHPfile pathbasenamestring function
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.