Q. Explain pathinfo() in PHP
In PHP, pathinfo()
is a function used to parse information about a file path. It returns an associative array containing information about the file path, such as the directory name, base name (file name without the directory), extension, and filename without extension.
Here's the basic syntax of pathinfo()
:
phparray pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
$path
: The file path to be parsed.$options
(optional): A bitmask of options to specify which elements of the path information to return. It defaults toPATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME
, which returns all available information.
The keys of the associative array returned by pathinfo()
are as follows:
dirname
: The directory component of the path.basename
: The base name (filename) of the path.extension
: The file extension (if any).filename
: The file name without the extension.
Example:
php$path = '/path/to/file.txt';
$pathInfo = pathinfo($path);
echo $pathInfo['dirname'];
// Output: /path/to
echo $pathInfo['basename'];
// Output: file.txt
echo $pathInfo['extension'];
// Output: txt
echo $pathInfo['filename'];
// Output: file
In this example, pathinfo()
parses the file path /path/to/file.txt
and returns an associative array with information about the directory name, base name, extension, and filename without extension.
pathinfo()
is commonly used when working with file paths to extract various components of the path dynamically within PHP applications. It's particularly useful for tasks such as manipulating file names, determining file extensions, or working with file paths in a platform-independent manner.
No comments:
Post a Comment