pathinfo
Returns information about a file path
Syntax
pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] ) : mixed
Parameters
path
The path to be parsed.
options
If present, specifies a specific element to be returned; one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME. If options is not specified, returns all available elements.
Return
If the options parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename. If options is present, returns a string containing the requested element.
Note: If the path has more than one extension, PATHINFO_EXTENSION returns only the last one and PATHINFO_FILENAME only strips the last one.
Note: If the path does not have an extension, no extension element will be returned.
Note: If the basename of the path starts with a dot, the following characters are interpreted as extension, and the filename is empty.
Examples
1
<? $path_parts = pathinfo('/www/htdocs/inc/lib.inc.php'); echo $path_parts['dirname'] . "\n"; echo $path_parts['basename'] . "\n"; echo $path_parts['extension'] . "\n"; echo $path_parts['filename']; ?>
/www/htdocs/inc lib.inc.php php lib.inc
2
<? $path = '/www/htdocs/inc/lib.inc.php'; echo pathinfo($path, PATHINFO_DIRNAME) . "\n"; echo pathinfo($path, PATHINFO_BASENAME) . "\n"; echo pathinfo($path, PATHINFO_EXTENSION) . "\n"; echo pathinfo($path, PATHINFO_FILENAME); ?>
/www/htdocs/inc lib.inc.php php lib.inc
3
<? $path_parts = pathinfo('/path/emptyextension.'); var_dump($path_parts['extension']); $path_parts = pathinfo('/path/noextension'); var_dump($path_parts['extension']); ?>
string(0) "" NULL
4
<? print_r(pathinfo('/some/path/.test')); ?>
Array ( [dirname] => /some/path [basename] => .test [extension] => test [filename] => )