In PHP, realpath() turns a messy file path into a clean, absolute one. It removes things like ./ and ../ and follows symbolic links.
The path must really exist on disk. If it does, you get the full real path back. If it does not, you get false, which makes realpath() a handy way to check a path and clean it in one step.
Syntax
Call realpath() with the values you need. Start with this line.
PHP
$full = realpath(__DIR__ . '/../config');
Call
What it does
realpath('.')
Returns the full path of the current working folder.
realpath(__DIR__ . '/../images')
Resolves the ../ part and returns the real absolute path of the images folder.
realpath('missing.txt')
Returns false because the file does not exist.
realpath() only works for paths that exist. It also resolves symbolic links, so the result may be different from the path you typed. On Windows it returns the path with backslashes.
Library declarations
PHP
realpath(string $path): string|false
Parameters
Parameter
Description
path
Required. The path you want to clean up. It can be relative or absolute, and may contain ./, ../ and symbolic links.
Return Value
Return Value
Description
string|false
Returns the canonical absolute path as a string, or false if the path does not exist or a folder in it cannot be read.
PHP Examples
Example 1 — Clean Up a Path with ./
Create a small file and ask realpath() to clean a path that contains ./.