PHP Filesystem pathinfo() Function
The PHP Filesystem pathinfo() function is used to return an array that contains information about a path. If the options parameter is not passed, an associative array containing the elements is returned: dirname, basename, extension (if any), and filename.
The pathinfo() function is aware of your computer's language and region settings. To properly handle file paths with special characters, use the 'setlocale()' function to pick the correct language and region.
Syntax
Below is the syntax of the PHP Filesystem pathinfo() function −
mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
Parameters
Below are the required and optional parameters of the pathinfo() function −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
$path(Required) It is the path to be parsed. |
| 2 |
$options(Optional) It is an integer that specifies what information to return.
|
Return Value
The function pathinfo() returns information about the path: either an associative array or a string, depending on options.
PHP Version
The pathinfo() function was first introduced as part of core PHP 4.0.3 and work well with the PHP 5, PHP 7 and PHP 8.
Example
Here is the basic example to see how the PHP Filesystem pathinfo() function is used to parse the information about the path.
<?php
print_r(pathinfo("/PhpProjects/simple.txt"));
?>
Output
Here is the outcome of the following code −
Array ( [dirname] => /PhpProjects [basename] => simple.txt [extension] => txt [filename] => simple )
Example
Here is the another example to show the usage of pathinfo() function to get the information about the given path also we will use here the optional parameter PATHINFO_BASENAME to get the base name from the path.
<?php
print_r(pathinfo("/PhpProjects/simple.txt", PATHINFO_BASENAME));
?>
Output
This will produce the following result −
simple.txt
Example
Here is one more example to the complete information about the given path inside the pathinfo() function by using all the optional parameters..
<?php $path = "/PhpProjects/myfile.txt"; // Get only the directory name $dirname = pathinfo($path, PATHINFO_DIRNAME); echo "The only directory name: ".$dirname; echo "\n"; // Get only the base name $basename = pathinfo($path, PATHINFO_BASENAME); echo "The Base name only: ".$basename; echo "\n"; // Get only the file extension $extension = pathinfo($path, PATHINFO_EXTENSION); echo "The file extension: ".$extension; echo "\n"; // Get only the file name without the extension $filename = pathinfo($path, PATHINFO_FILENAME); echo "The filename without extension: ".$filename; ?>
Output
This will generate the below output −
The only directory name: /PhpProjects The Base name only: myfile.txt The file extension: txt The filename without extension: myfile
Notes
Below are some important points to keep in mind while working with the pathinfo() function −
- The pathinfo() operates naively on the given string, uninformed of the real filesystem or path components, like ".."
- On Windows systems, the \ character is used as a directory separator. On other systems, it will be treated like any other character.
Summary
The pathinfo() method is a built-in function to parse the information about the given path. It is very helpful to get the exact information about the particular path or file like its extension, its folder location, its name.