272 of 410 menu

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

realpath(path);

Example

Get the absolute path to the current file:

<?php echo realpath(__FILE__); ?>

Code execution result (example):

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

Example

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

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

Code execution result:

false

Example

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

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

Code execution result (example):

'/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
huptrobnfr