PHP realpath() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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');
CallWhat 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

ParameterDescription
pathRequired. The path you want to clean up. It can be relative or absolute, and may contain ./, ../ and symbolic links.

Return Value

Return ValueDescription
string|falseReturns 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 ./.

PHP
<?php
$dir  = sys_get_temp_dir();
$file = $dir . '/realpath_demo.txt';
file_put_contents($file, 'hi');

$messy = $dir . '/./realpath_demo.txt';

echo basename(realpath($messy)), PHP_EOL;
echo realpath($messy) === realpath($file) ? 'Same file' : 'Different file', PHP_EOL;

unlink($file);

How It Works

  1. sys_get_temp_dir() gives a folder where PHP can safely create a test file.
  2. $messy has a . / in the middle, which means the current folder and does nothing.
  3. realpath() removes the . / and returns the clean absolute path, so basename() prints the file name.
  4. Both paths resolve to the same real file, so the comparison prints Same file.

Example 2 — A Path That Does Not Exist

realpath() returns false when any part of the path is missing, even if the final path looks fine.

PHP
<?php
$path = sys_get_temp_dir() . '/no_such_folder/../file.txt';

$real = realpath($path);

if ($real === false) {
    echo "realpath() returned false", PHP_EOL;
} else {
    echo "Found: $real", PHP_EOL;
}

How It Works

  1. The path goes into no_such_folder and then back out with . ./.
  2. realpath() needs every folder in the path to exist, and no_such_folder does not.
  3. So it returns false instead of guessing a result.
  4. The === false check prints a clear message.

Example 3 — Block Paths Outside a Base Folder

Use realpath() to make sure a requested file stays inside an allowed folder.

PHP
<?php
$base = sys_get_temp_dir() . '/rp_base';
@mkdir($base);
file_put_contents($base . '/a.txt', 'A');
file_put_contents(sys_get_temp_dir() . '/rp_outside.txt', 'X');

$requests = ['a.txt', '../rp_outside.txt'];

foreach ($requests as $name) {
    $full = realpath($base . '/' . $name);
    $safe = $full !== false
        && str_starts_with($full, realpath($base) . DIRECTORY_SEPARATOR);
    echo $name, ': ', $safe ? 'allowed' : 'blocked', PHP_EOL;
}

unlink($base . '/a.txt');
rmdir($base);
unlink(sys_get_temp_dir() . '/rp_outside.txt');

How It Works

  1. A base folder and two files are created, one inside the base and one outside.
  2. realpath() resolves each requested name, including the . ./ part.
  3. str_starts_with() checks that the real path begins with the base folder.
  4. The second request escapes the base folder, so it is blocked. The cleanup lines remove the test files.

Frequently Asked Questions

It returns the clean, absolute path of a file or folder. It removes ./ and ../ parts and resolves symbolic links, but only for paths that exist.
It returns false. Compare the result with === false so you do not treat a failed lookup as a real path.
No. It only works with existing files and folders. For a new file, build the path yourself with dirname() and realpath() on the parent folder.
Yes. It helps stop directory traversal attacks. Resolve the user path with realpath() and check that the result starts with your allowed base folder.
dirname() only cuts a string and does not check the disk. realpath() asks the file system, resolves links, and returns false if the path is missing.

Next: realpath_cache_get()

realpath_cache_get() returns the entries that are currently stored in PHP's realpath cache.

realpath_cache_get() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful