realpath()

realpath() — Returns canonicalized absolute pathname.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, 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

realpath() expands all symbolic links and resolves references to /./, /../ and extra / characters in the input path and returns the canonicalized absolute pathname.

Signature

realpath(string $path): string|false

Parameters

ParameterTypeWhat it takes
$pathstringThe path being checked. Note: Whilst a path must be supplied, the value can be an empty string. In this case, the value is interpreted as the current directory.

Return value

Returns the canonicalized absolute pathname on success. The resulting path will have no symbolic link, /./ or /../ components. Trailing delimiters, such as \ and /, are also removed. realpath() returns false on failure, e.g. if the file does not exist. Note: The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return false. The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return false. Note: For case-insensitive filesystems realpath() may or may not normalize the character case. For case-insensitive filesystems realpath() may or may not normalize the character ...

Example

A runnable snippet:

chdir('/var/www/'); echo realpath('./../../etc/passwd') . PHP_EOL; echo realpath('/tmp/') . PHP_EOL;

Output:

/etc/passwd /tmp

See also

basename(), dirname(), pathinfo()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page realpath() (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.