pathinfo()

pathinfo() — Returns information about a file path.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

pathinfo() returns information about path: either an associative array or a string, depending on flags.

Signature

pathinfo(string $path, int $flags = PATHINFO_ALL): array|string

Parameters

ParameterTypeWhat it takes
$pathstringThe path to be parsed.
$flagsintIf present, specifies a specific element to be returned; one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.

Return value

If the flags parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename. Note: If the path has more than one extension, PATHINFO_EXTENSION returns only the last one and PATHINFO_FILENAME only strips the last one. (see first example below). If the path has more than one extension, PATHINFO_EXTENSION returns only the last one and PATHINFO_FILENAME only strips the last one. (see first example below). Note: If the path does not have an extension, no extension element will be returned (see second example below). If the path does not have an extension, no extension element will be returned (see second example ...

Example

A runnable snippet:

$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'], "\n";

Output:

/www/htdocs/inc lib.inc.php php lib.inc

See also

dirname(), basename(), parse_url(), realpath()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page pathinfo() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.