The realpath Function

The realpath function converts a relative path to an absolute one, resolving all symbolic links and references to parent directories (..). The function accepts one parameter - the path to a file or directory. If the path does not exist, the function returns false.

Syntax

phprealpath(path);

Example

Get the absolute path to the current file:

php<?php
  echo realpath(__FILE__);
?>

Code execution result (example):

php'/var/www/project/index.php'

Example

Try to get the path to a non-existent file:

php<?php
  $res = realpath('nonexistent/file.txt');
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Assume that /home/user/link points to /var/www. Resolve the path with symbolic links:

php<?php
  echo realpath('/home/user/link/project');
?>

Code execution result (example):

php'/var/www/project'

See Also

  • the pathinfo function,
    which returns information about a path
  • the basename function,
    which returns the filename
  • the dirname function,
    which returns the directory name