PHP basename() function
In this chapter you will learn:
- What is PHP basename() function
- Syntax for PHP basename() function
- PHP basename() function Parameter
- PHP basename() function return value
- Example - get the filename part of a path
Definition
To get the filename part of a path, you can use the basename() function.
Syntax
PHP basename() function has the following syntax.
basename(filePath,extension)
Parameter
PHP basename() function takes a path as its first parameter and, optionally, an extension as its second parameter.
Return
The return value is the name of the file without the directory information.
If the filename has the same extension as specified, the extension is taken off also.
Example
Get the filename part of a path
<?PHP//from java 2 s . c o m
$filename = basename("/home/somefile.txt");
echo $filename . "\n";
$filename = basename("/home/somefile.txt", ".php");
echo $filename . "\n";
$filename = basename("/home/somefile.txt", ".txt");
echo $filename . "\n";
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What is PHP chdir() function
- PHP chdir() function syntax
- PHP chdir() function parameter
- PHP chdir() function return value
- Example - Change the working directory using chdir()
Home » PHP Tutorial » PHP File Functions