
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.