Using PHP basename() Function to Extract File Names from Paths

This article explains the PHP basename() function, its syntax, parameters, and demonstrates its usage through multiple examples showing how to extract file names from absolute and relative paths and optionally remove suffixes.

php Courses
php Courses
php Courses
Using PHP basename() Function to Extract File Names from Paths

In PHP programming, it is often necessary to manipulate file paths, and the basename() function provides a quick and convenient way to obtain the filename portion of a path. This article details the functionality and usage of the basename() function, illustrated with code examples.

The basic syntax of the basename() function is as follows:

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

Parameter description:

$path

: required, the file path, which can be relative or absolute. $suffix: optional, the file extension to be removed.

Functionality:

Retrieves the filename part of a path.

The following examples demonstrate how to use the basename() function.

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(), the returned filename is assigned to $filename, and printed, resulting in index.php.

Example 2:

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

Output: pic.jpg Here a relative path "images/pic.jpg" is provided to basename(), which returns the filename pic.jpg.

Example 3:

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

Output: index In this case, besides the path, an optional suffix .php is specified; basename() removes this suffix from the filename, returning index.

The basename() 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 may vary across operating systems: Windows uses backslashes ("\\") as separators, while Linux and macOS use forward slashes ("/"). Therefore, be aware of the OS-specific path separators when using basename().

Summary

In PHP development, the basename() function is highly useful for easily extracting the filename from a file path. It can be applied in scenarios such as file manipulation, URL handling, and file uploads, improving development efficiency and code readability when mastered and used flexibly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

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

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.