PHP Filesystem basename() Function
The PHP Filesystem basename() function allows you to extract only the name of a file from a whole path. It is commonly used to extract a filename from a complete path string.
The primary goal of basename() is to extract the path's trailing name component while ignoring previous directory names.
Assume you know a file's entire address, like "C:/documents/picture.jpg". If you simply want the "picture.jpg" filename, you can use basename() for this.
Syntax
Below is the syntax of the PHP Filesystem basename() function −
string basename ( string $path [, string $suffix ] )
Parameter
The parameters are needed to use the basename() function are mentioned below −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
path(Required) The path string from which the filename need to extract. |
| 2 |
suffix(Optional) This suffix will be removed from the extracted filename. |
Return Value
It returns the extracted filename as a string.
PHP Version
The basename() function is part of the PHP core and is available in PHP 4, PHP 5, PHP 7, and PHP 8.
Example
In this example we will use the PHP Filesystem basename() function to simply extract the filename provided in the path and print the filename with extension and without extension. So we will use basename() to remove the filename component from the path before giving it to the $file variable.
<?php // specify the directory path here $path = "/Applications/XAMPP/xamppfiles/htdocs/mac/index.php"; // $file is set to "index.php" $file = basename($path); // This will print "Full file name: index.php" echo "Full file name: " . $file . "<br>"; // $file is set to "index" $file = basename($path, ".php"); // This will print "File name without extension: index" echo "File name without extension: " . $file; ?>
Output
This will produce the following result −
Full file name: index.php File name without extension: index
Example
Now we will handle different file extensions using basename() function. So here we can handle different file extension using this function. The basename() function will get the file name without its extension for each path mentioned in the code.
<?php // specify the directory path here $path1 = "/Applications/XAMPP/xamppfiles/htdocs/mac/index.php"; $path2 = "/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt"; $path3 = "/Applications/XAMPP/xamppfiles/htdocs/mac/logo.gif"; // Filename without extension: php echo "Filename without extension: " . basename($path1, ".php") . "<br>"; // Filename without extension: text echo "Filename without extension: " . basename($path2, ".txt") . "<br>"; // Filename without extension: gif echo "Filename without extension: " . basename($path3, ".gif") . "<br>"; ?>
Output
This will produce the below outcome −
Filename without extension: index Filename without extension: myfile Filename without extension: logo
Example
Handling URLs most likely refers to a specific example of how to handle URLs that include query parameters. So parse the URL to get the path using parse_url() function, then use basename() to extract and print the file name.
<?php // specify the directory path here $url = "/Applications/XAMPP/xamppfiles/htdocs/mac/index.php?version=1"; // Parse the URL to get the path $parsed_url = parse_url($url, PHP_URL_PATH); // Use basename() on the path to get the file name $filename = basename($parsed_url); echo "Filename: " . $filename; ?>
Output
This will lead to the following outcome −
Filename: index.php
Example
So the basename() function can also handle the filenames with multiple dots in it. So simply we need to pass the directory path in the basename() to have this functionality.
Below is the simple demo of this example −
<?php // specify the directory path here $path = "/Applications/XAMPP/xamppfiles/htdocs/mac/file.with.dots.txt"; // use basename() function to extract the filename $filename = basename($path); // Output: Filename: file.with.dots.txt echo "Filename: " . $filename; ?>
Output
This will produce the following result −
Filename: file.with.dots.txt
Summary
The PHP basename() function is a really useful functionality used to extract the file name part of a given path string. Actually, it offers an option to the user for removing any specified suffix from being extracted at the end.