On this information, we’ll talk about the usage of the pathinfo() operate in PHP.
What’s the pathinfo() Operate in PHP?
The pathinfo() is a helpful technique in PHP that accepts the file path as a parameter and returns its part. This operate is beneficial for file manipulation and dealing with in PHP. The returned string or an associative array has the next info associated to the file path:
- directoryname
- filename
- extension
- basename
Syntax
The syntax of utilizing the pathinfo() operate is as follows:
This operate accepts the 2 parameters path and choices. The path is the trail of the file for which you need to get the knowledge and choices are elective parameters to specify the return info. In the event you don’t use any flag, this operate returns all of the elements of your file.
The next are the legitimate flags that can be utilized on this operate:
Choice | Description |
---|---|
PATHINFO_DIRNAME | It returns the listing or folder title of the file |
PATHINFO_EXTENSION | It returns the extension of the file |
PATHINFO_FILENAME | It returns the file title with out an extension |
PATHINFO_BASENAME | It returns the bottom title |
Find out how to Use the pathinfo() Operate in PHP?
The next instance code snippets illustrate the utilization of the pathinfo() operate in PHP:
Instance 1
The beneath instance makes use of the pathinfo() operate to get all of the details about the file path:
Instance 2
Within the following instance, we’ve specified the part of the file path. It’s going to return the basename of the file solely:
$path = ‘/pattern/myfile.txt’;
$basename = pathinfo($path, PATHINFO_BASENAME);
echo $basename;
?>
Instance 3
Right here is one other instance that extracts the trail info of a picture file within the PHP code.
$file_path = ‘pattern/picture.png’;
$path_info = pathinfo($file_path);
echo “The listing title is: “ .$path_info[‘dirname’] .“<br>”;
echo “The basename is: “ .$path_info[‘basename’] .“<br>”;
echo “The extension of file is: “ .$path_info[‘extension’] .“<br>”;
echo “The filename is: “ .$path_info[‘filename’] .“<br>”;
?>
Backside Line
You should utilize the pathinfo() operate in PHP to get the basename, listing title, filename, and extension of the file. This operate permits the builders to extract essential details about the file path. The returned info can be utilized to examine the file title, its kind, and the situation of the file. We now have mentioned the utilization of the pathinfo() operate of PHP within the above part of this information.