PHP Function Reference

PHP realpath() Function



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

Syntax

realpath(path)

Parameters

path Required. Specify the path being checked. If empty string is provided, it 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. It returns false on failure.

Note: The running script must have executable permissions on all directories in the hierarchy, otherwise this function will return false.

Example: realpath() example

The example below shows the usage of realpath() function.

<?php
//changing the current directory
chdir('/var/www/');

//getting the absolute pathnames
echo realpath('./../../etc/passwd') . PHP_EOL;
echo realpath('/tmp/') . PHP_EOL;
?>

The output of the above code will be similar to:

/etc/passwd
/tmp

Example: realpath() on Windows

Consider one more example where this function is used to get canonicalized absolute pathname on Windows.

<?php
//getting the absolute pathnames
echo realpath('/temp/test.txt'), PHP_EOL;
echo realpath('C:\Program Files\\'), PHP_EOL;
?>

The output of the above code will be similar to:

C:\WINDOWS\temp\test.txt
C:\Program Files

❮ PHP Filesystem Reference