Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP realpath() function

Reference for PHP realpath(): turn a relative or messy path into a canonical absolute path, what it returns for missing files, and how to use it to block path traversal.

The realpath() function turns a path into its canonical absolute form. It resolves . and .. segments, collapses repeated slashes, follows symbolic links, and returns the final path as a string. If the path does not exist, it returns false.

Because it touches the file system, realpath() only works for things that exist right now. That is also what makes it useful as a safety check: after resolving a user-supplied name, you can test whether the result still lies inside the folder you meant to serve.

PHP
<?php
$full = realpath(__DIR__ . '/../config/app.php');
if ($full === false) {
    echo "No such file";
}

Try it

PHP Normalizing a messy path

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Blocking path traversal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
realpath(string $path): string|false
Name Description
$path The path to resolve. It may be relative (resolved against the current working directory) or absolute.

Return value: the canonical absolute path as a string, without a trailing slash. Returns false on failure, most often because the path does not exist or a directory on the way is not accessible.

Errors / Exceptions: none. Failure is reported only through the false return value.

Usage notes

  • The path must exist. realpath() cannot normalize a file you are about to create. Resolve the parent directory instead and append the file name yourself.
  • Always compare with false. Test the result with === false so the failure case is explicit before you use the path.
  • Containment check: compare with str_starts_with($target, $base . DIRECTORY_SEPARATOR). Checking only str_starts_with($target, $base) would accept a sibling such as /data/public-old when the base is /data/public.
  • Symlinks are followed. The result is the real location, which may be outside the folder where the link lives. That is exactly what you want for security checks.
  • Relative paths depend on the working directory. Prefer __DIR__ . '/file' over a bare relative path, because the working directory differs between web and command-line runs.
  • The empty string resolves to the current working directory.
  • Results can vary by platform. On Windows, separators come back as backslashes and the drive letter is preserved.
  • Related helpers: pathinfo(), basename() and dirname() split a path without touching the disk, so they work on paths that do not exist.