- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP basename() Function | Get Filename from Path
The PHP basename() function is used to get the file name from the specified path. For example:
<?php $path = "C:/Users/DEV/fresherearth.com/myfile.txt"; echo basename($path); ?>
The output of the above PHP example is:
And here is the snapshot of the directory and path along with the file used in the above example:
PHP basename() Syntax
The syntax of the basename() function in PHP is:
basename(path, suffix)
The second, or suffix, parameter is optional. This parameter is used when we need to remove the extension of the file so that we only get the file name. For example:
<?php $path = "C:/Users/DEV/fresherearth.com/myfile.txt"; echo basename($path, ".txt"); ?>
This code defines a variable $path that contains the file path "C:/Users/DEV/fresherearth.com/myfile.txt". The path uses forward slashes to separate directories, which is the recommended practice for compatibility across different platforms. The function basename() is then called with two arguments: $path and ".txt." The basename() function parses the path and returns the file name as a string. The second argument, ".txt," instructs the function to remove the ".txt" extension from the file name if it is present.
The output of the above PHP example is:
Advantages of the basename() function in PHP
- Simple to use: The basename() function is straightforward and simple to use. It takes a path as an argument and returns the file name as a string.
- Results that are consistent across platforms, operating systems, and file systems are produced by the basename() function.
- Security: The ability of basename() to extract the filename from a user-supplied path can be helpful for preventing directory traversal attacks.
- Flexibility: basename() can be used to extract the filename from a path, even if it includes a query string or a fragment identifier.
Disadvantages of the basename() function in PHP
- basename() does not support multibyte characters, which can be problematic for speakers of languages like Chinese, Japanese, or Korean.
- basename() only returns the filename and offers no additional details about the file, such as its size, extension, or modification date.
- When working with file paths that use backslashes rather than forward slashes, basename() can behave differently on different platforms.
- basename() is only appropriate for straightforward operations, such as obtaining the filename from a path, and is not suitable for complex operations. A more potent function or library may be required for trickier operations, like changing the file path.
« Previous Tutorial Next Tutorial »